Naming MySQL tables with PHP variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

Naming MySQL tables with PHP variables

Post 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?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

Post by bwv2 »

Thanks. I caught my error. It was not in the syntax but elsewhere. I appreciate the help.
Post Reply