table prefix parser

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
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

table prefix parser

Post by SidewinderX »

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

Code: Select all

$prefix = "ion_";
$sql = "SELECT * FROM `#__blog`";
$sql = str_replace("#__", $ion, $sql);
//outputs: $sql = "SELECT * FROM `ion_blog`';
 
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 `ion_` FROM `ion_blog`';
//should be: $sql = "SELECT `#__` FROM `ion_blog`';
 
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: table prefix parser

Post by watson516 »

Im not sure if this is the proper way of doing things but you could start replacing after the FROM in your string. You would probably have to split up your string and then fix it afterwards but its doable.

Or, you could just assume that no one in their right mind would name a table '#__' and save some coding.

Or, what about doing something like...

Code: Select all

<?php
$prefix = "ion_";
$sql = "SELECT * FROM `".$prefix."blog`";
?>
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: table prefix parser

Post by SidewinderX »

watson516 wrote:Im not sure if this is the proper way of doing things but you could start replacing after the FROM in your string. You would probably have to split up your string and then fix it afterwards but its doable.
Thanks. It is not a bad idea, but it the table name doesn't always come after FROM. For example: INSERT INTO `table_name`. I suppose I could create an array of keywords the table name would appear after, but I can't help but think there is a more elegant solution. I am interested in what other people have to say.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: table prefix parser

Post by requinix »

Think about it this way:

Why the heck would someone name something "#__"?
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: table prefix parser

Post by SidewinderX »

tasairis wrote:Think about it this way:

Why the heck would someone name something "#__"?
I don't know, but I'm the type of programmer who looks both ways while crossing a one way street. As far as I know, a table can be named #__ so I am going to assume someone using my code might name it that.
Post Reply