I currently have a list of defined constants and a function that regex'es every pulled MySQL string and looks for things like CLIENT_NAME, LOCAL_API_ADDRESS and auto-changes it.
// several fields
define ('CLIENT_NAME', '...');
define ('LOCAL_API_ADDRESS', '...');
...
The thing is, as my app is getting larger I feel this is pretty inefficient and bound to loose strings (forgetting to regex here or there).
How do you guys cope with this?
Literals (Constants) Literals are the notations or idea to represent/express a non-changing value. In MySQL, literals are similar to the constant. We can use a literal while declaring a variable or executing the queries.
You can access any user-defined variable without declaring it or initializing it. If you refer to a variable that has not been initialized, it has a value of NULL and a type of string.
By default 151 is the maximum permitted number of simultaneous client connections in MySQL 5.5. If you reach the limit of max_connections you will get the “Too many connections” error when you to try to connect to your MySQL server. This means all available connections are in use by other clients.
MySQL has had User-defined variables since v3.23.6, but they...
...are connection-specific. That is, a user variable defined by one client cannot be seen or used by other clients. All variables for a given client connection are automatically freed when that client exits.
Effectively, there's no convention in MySQL for storing constants.
You could store the values in a table, but things like MySQL's REGEXP might require dynamic SQL (using MySQL's Prepared Statements) in order to take advantage of the setup.
Honestly, unless you are doing this an abnormally large number of times, it's not that bad to do this. Many templating engines uses regex search and replaces. Most of the overhead is in loading the regex engine the first time. Multiple queries aren't that expensive.
You are better off ensuring that your REGEX's are optimized.
Be careful handling data like this. If you pull data from the database that overrides certain constants, you might end up with security issues !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With