table prefix parser
Posted: Sat Jan 03, 2009 1:35 pm
In my configuration file there is a variable that allows the user to specify the table prefix for the installation of my software. In the event they would like two installations of this software in the same database, they can simply change the table prefix. From my encounters with other CMS's, they use a "wild card" in their SQL so the table prefix is not hard coded. I am aware that str_replace or preg_replace can replace the wild card with the prefix selected in the config file, but what should the wild card be? Joomla! uses #__, but what prevents a column or table from being called that? Sure something like this might work
but what if there was a column called #__
Code: Select all
$prefix = "ion_";
$sql = "SELECT * FROM `#__blog`";
$sql = str_replace("#__", $ion, $sql);
//outputs: $sql = "SELECT * FROM `ion_blog`';
Code: Select all
$prefix = "ion_";
$sql = "SELECT `#__` FROM `#__blog`";
$sql = str_replace("#__", $ion, $sql);
//outputs: $sql = "SELECT `ion_` FROM `ion_blog`';
//should be: $sql = "SELECT `#__` FROM `ion_blog`';