Page 1 of 1

Naming MySQL tables with PHP variables

Posted: Mon Aug 15, 2005 12:19 pm
by bwv2
I'm having issues trying to name tables using php vars. I want to have tables containing user data for many different users (yes, I know about sessions, but this is for a lot of data). I would like to have the table name start with the username so that the program will know where each user's data is stored. I am currently trying the following code without good results:

Code: Select all

$sql="DROP TABLE IF EXISTS ".$_SESSION['username']."Table";
$result=mysql_query($sql, $conn);
$sql="CREATE TABLE ".$_SESSION['username']."Table (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(40), quantity INT)";
$result=mysql_query($sql,$conn);
For a user with username "bob", the table should be called "bobTable". However, I'm getting errors saying "error: mydatabase.table" does not exist. This tells me that the code isn't even recognizing the PHP variable at all and is just naming it "table". Is there an error in my syntax?

Posted: Mon Aug 15, 2005 12:21 pm
by nielsene
try adding a print_r($_SESSION), ie make sure the session is setup as you think it should. Turning error_reporting on and such as well might help.

I don't see an obvious syntax error.

Posted: Mon Aug 15, 2005 12:27 pm
by bwv2
Thanks. I caught my error. It was not in the syntax but elsewhere. I appreciate the help.