Noob question: '[| |]' in MySQL queries

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RCA86
Forum Commoner
Posts: 32
Joined: Thu Mar 10, 2011 1:03 pm

Noob question: '[| |]' in MySQL queries

Post by RCA86 »

Hi guys, this is a total noob question, but it's been eating away at my brain for the past few months. :banghead:

In a MySQL query, there's this code: ' ... [|PREFIX|]products ...'. Now I know exactly what this does, it inserts the prefix for the table, as this is globally configurable, but I don't know if it's PHP, MySQL, or where/how you can define this.

Put me out of my misery? :D
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Noob question: '[| |]' in MySQL queries

Post by twinedev »

that would be handled by the php program to do the replacement, most likely a custom defined function. ex:

Code: Select all

// The following would most likely get set in a config/settings file for the app.
$strTablePrefix = 'mytable_';

function runQuery($sql) {
    return mysql_query(str_replace('[|PREFIX|]',$strTablePrefix,$sql);
}

$rsProduct = runQuery('SELECT * FROM [|PREFIX|]products WHERE ProductID = 3');
The main purpose would be to just make the code more readable and easier to type since you don't have to close quote (or use variables withing double quotes).

The above could have just been written as:

Code: Select all

// The following would most likely get set in a config/settings file for the app.
$strTablePrefix = 'mytable_';

$rsProduct = mysql_query('SELECT * FROM '.$strTablePrefix.'products WHERE ProductID = 3');
RCA86
Forum Commoner
Posts: 32
Joined: Thu Mar 10, 2011 1:03 pm

Re: Noob question: '[| |]' in MySQL queries

Post by RCA86 »

Ah, so it's not any kind of special syntax after all. Makes sense!

Thanks!
Post Reply