For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > August 2004 > Re: [PHP-DB] letting a table name be a constant









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] letting a table name be a constant
Justin Patrin

2004-08-23, 8:55 pm

On Mon, 23 Aug 2004 17:06:40 -0700, Ben Galin <news@bens.ws> wrote:
> Hello,
>
> Regarding this code:
> [PHP code]
> // This works
> $name="name";
> $table="mytable";
> $mysql_connect("localhost","","") or die("Error: ".mysql_error());
> $mysql_select_db("mydb") or die("Error: ".mysql_error());
> $mysql_query("INSERT INTO $table (`id`,`name`) VALUES ('','$name')");
> [/PHP code]
>
> I want to define() TABLE as a constant instead of using the variable
> $table
>
> [PHP code]
> // This runs without an error but does *not* insert a new row
> $name="name";
> define("TABLE", "mytable");
> $mysql_connect("localhost","","") or die("Error: ".mysql_error());
> $mysql_select_db("mydb") or die("Error: ".mysql_error());
> $mysql_query("INSERT INTO TABLE (`id`,`name`) VALUES ('','$name')");
> [/PHP code]
>
> Changing the query line to
> $mysql_query("INSERT INTO `TABLE` (`id`,`name`) VALUES ('','$name')");


Well, of course PHP isn't going to change your strings. That would be
a bit intrusive and make some things real hard to do. C won't even do
this. Here's what you want:
$mysql_query('INSERT INTO `'.TABLE.'` (`id`,`name`) VALUES ("","'.$name.'")');

> or
> $mysql_query("INSERT INTO 'TABLE' (`id`,`name`) VALUES ('','$name')");
> has no effect.
>
> I also tried to
> define("TABLE", "`mytable`");
> which, too, didn't work.
>
> Would appreciate any advice,
> Ben
>



--
DB_DataObject_FormBuilder - The database at your fingertips
http://pear.php.net/package/DB_DataObject_FormBuilder

paperCrane --Justin Patrin--
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com