Forum Moderators: coopster

Message Too Old, No Replies

Adding prefixes to database tables!

not working but why?

         

dreamcatcher

11:21 am on Oct 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Guys,

I`ve done this script and for anyone using it with only one database, I want prefixes before the table names.

$db_prefix = "mw_";

For an example the first table that gets created is called entries.

$query = "CREATE TABLE entries (
id INT(5) NOT NULL auto_increment,
date VARCHAR(40),
caldate DATE NOT NULL,
subject VARCHAR(50) NOT NULL,
message TEXT,
PRIMARY KEY(id)) TYPE=MyISAM";
$result = mysql_query($query);

When I click install, this adds to the database ok. So I changed that to:

$query = "CREATE TABLE " . $db_prefix . " entries (
id INT(5) NOT NULL auto_increment,
date VARCHAR(40),
caldate DATE NOT NULL,
subject VARCHAR(50) NOT NULL,
message TEXT,
PRIMARY KEY(id)) TYPE=MyISAM";
$result = mysql_query($query);

Now what happens is I get the following error message:

You have an error in your SQL syntax near 'entries ( id INT(5) NOT NULL auto_increment, date VARCHAR(40),' at line 1

Can anyone see what the problem is?

Thank you!

RobinC

12:05 pm on Oct 11, 2003 (gmt 0)

10+ Year Member



Only thing I can see is you're putting a space after the $db_prefix - try changing it from -
$query = "CREATE TABLE " . $db_prefix . " entries (
- to -
$query = "CREATE TABLE " . $db_prefix . "entries (

See?

lorax

12:43 pm on Oct 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I often get tripped up over when to concantenate a string like you did with " . $db_prefix . " versus just placing the var within the string. Try just using the var without the concantenation like so 'CREATE TABLE $db_prefix entries'.

dreamcatcher

2:11 pm on Oct 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys. I`ve tried both and the same error appears. Like I say, if I remove the $db_prefix and just have say entries it works fine.

I`ve been trying to wrap my head round why this isn`t working and I just keep drawing a blank. I use an e-card script on my site that uses exactly the same method and that works fine.

Strange.

dreamcatcher

2:22 pm on Oct 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh, you know what? I`ve got it working. RobinC was on the right track, only it was the gaps with the concantenation too.

" . $db_prefix . " entries

should be:

".$db_prefix."entries

No gaps.

Phew, thank god for that.

:)

lorax

6:58 pm on Oct 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



LOL - glad you found it. I've spent many hours chasing down silly mistakes like that. More than I care to think about. :)

dreamcatcher

9:44 pm on Oct 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I used to keep telling myself it was all part of the fun in learning. I`m over that phase now! haha!