r00tk1LL Posted August 12, 2007 Share Posted August 12, 2007 I am trying to create a dynamic table with php/msql and I keep running into a syntax problem. I need to use a variable for the table name but if I do this: $table = 'CREATE TABLE `$var` (' . ' `id` INT(15) NOT NULL AUTO_INCREMENT PRIMARY KEY, ' . ' `pics` TEXT NOT NULL, ' . ' `thumbs` TEXT NOT NULL' . ' )' . ' ENGINE = myisam;'; The table is created called $var literally Now if I use double quotes like this: $table = "CREATE TABLE `$var` (' . ' `id` INT(15) NOT NULL AUTO_INCREMENT PRIMARY KEY, ' . ' `pics` TEXT NOT NULL, ' . ' `thumbs` TEXT NOT NULL' . ' )' . ' ENGINE = myisam;"; The value of the variable is seen, but no table is created I need to find a way to create this table with the value that is stored in $var. Can someone help me out? Quote Link to comment https://forums.phpfreaks.com/topic/64555-create-table-syntax-help/ Share on other sites More sharing options...
cooldude832 Posted August 12, 2007 Share Posted August 12, 2007 dynamic table creation is a no no unless you can justify it. Quote Link to comment https://forums.phpfreaks.com/topic/64555-create-table-syntax-help/#findComment-321796 Share on other sites More sharing options...
hitman6003 Posted August 12, 2007 Share Posted August 12, 2007 You have to escape from single quotes when you want the value of a variable to be included... <?php $table = 'CREATE TABLE `' . $var . '` (' . ' `id` INT(15) NOT NULL AUTO_INCREMENT PRIMARY KEY, ' . ' `pics` TEXT NOT NULL, ' . ' `thumbs` TEXT NOT NULL' . ' )' . ' ENGINE = myisam'; ?> With that said, as was stated above, there is always a better way of doing something than creating a table based on user input. Not only does it open up gaping security holes, but it clutters your database. Quote Link to comment https://forums.phpfreaks.com/topic/64555-create-table-syntax-help/#findComment-321805 Share on other sites More sharing options...
r00tk1LL Posted August 12, 2007 Author Share Posted August 12, 2007 What I'm trying to do here is allow users to upload pictures to a dir, then create a table that sorts their pictures. I.E. Once a user uploads a picture, an original an thumbnail are copied to a dir, then a table is created with their username and it will store the pic url's, date created, and picture comment text Is there a better way? Quote Link to comment https://forums.phpfreaks.com/topic/64555-create-table-syntax-help/#findComment-321813 Share on other sites More sharing options...
cooldude832 Posted August 12, 2007 Share Posted August 12, 2007 yes used a centralized table with a field for the UserID of the pics owner. Tables are meant to hold data that can be compared aganist its self (on a row by row basis) and data that will have all the relative same use in output. For example if you wanted to store a schedule that had location, time, and people you would probably want to make your locatiosn be 1 table and people another then a table for the schedule that would link to the other ones. The key to any good table strucure is linking tables together and not overloaded/underloading tables. Some times storing a link to another tablse entry of something is best, others it by be best just to store those entries duplicated in table B. Quote Link to comment https://forums.phpfreaks.com/topic/64555-create-table-syntax-help/#findComment-321825 Share on other sites More sharing options...
r00tk1LL Posted August 12, 2007 Author Share Posted August 12, 2007 Can you give an example of linking tables, syntax wise? Quote Link to comment https://forums.phpfreaks.com/topic/64555-create-table-syntax-help/#findComment-321835 Share on other sites More sharing options...
keeB Posted August 12, 2007 Share Posted August 12, 2007 CREATE TABLE users ( userId primary key int auto_increment not null, . . ) CREATE TABLE images ( imageId primary key int auto_increment not null, userId int, path text, description text, . . ) Then, to query SELECT * FROM users u INNER JOIN images i on i.userId = u.userId where i.userId='$userId' Something like that. You can read up on relational/normalized database creation, which is what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/64555-create-table-syntax-help/#findComment-321845 Share on other sites More sharing options...
r00tk1LL Posted August 12, 2007 Author Share Posted August 12, 2007 Ok, I'm working on centralizing the pics table right now. I will keep this thread open until I get it all working. Quote Link to comment https://forums.phpfreaks.com/topic/64555-create-table-syntax-help/#findComment-321849 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.