Re: letting a table name be a constant
| From: | Justin Patrin | Date: | Tue, 24 Aug 2004 00:12:09 +0000 |
| Subject: | Re: letting a table name be a constant | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-35860@lists.php.net to get a copy of this message | ||
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--