Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constants and mysql, best practices

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?

like image 881
Frankie Avatar asked Jun 20 '10 21:06

Frankie


People also ask

What are MySQL constants?

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.

Can we declare a constant without an initial value in MySQL?

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.

How many concurrent queries can MySQL handle?

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.


3 Answers

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.

like image 144
OMG Ponies Avatar answered Nov 05 '22 22:11

OMG Ponies


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.

like image 35
Aaron Harun Avatar answered Nov 05 '22 23:11

Aaron Harun


Be careful handling data like this. If you pull data from the database that overrides certain constants, you might end up with security issues !

like image 2
wimg Avatar answered Nov 05 '22 23:11

wimg