Re: letting a table name be a constant

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 23 Aug 2004 17:06:40 -0700, Ben Galin <news@xxxxxxx> 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--

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux