r00tk1LL Posted August 11, 2007 Share Posted August 11, 2007 Here's a simple syntax question, how would I use a variable for a table name in this query? $table = 'CREATE TABLE `$var` //How should I use this variable?? (' . ' `id` INT(15) NOT NULL AUTO_INCREMENT PRIMARY KEY, ' . ' `pics` TEXT NOT NULL, ' . ' `thumbs` TEXT NOT NULL' . ' )' . ' ENGINE = myisam;'; Thanks Quote Link to comment https://forums.phpfreaks.com/topic/64447-create-table-var-help/ Share on other sites More sharing options...
marcus Posted August 11, 2007 Share Posted August 11, 2007 Try putting the table variable inside double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321316 Share on other sites More sharing options...
Fadion Posted August 11, 2007 Share Posted August 11, 2007 $var = 'tableName'; $table = "CREATE TABLE $var"; Inside single quotes it interprets $var as a string so it prings '$var'. Quote Link to comment https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321320 Share on other sites More sharing options...
r00tk1LL Posted August 11, 2007 Author Share Posted August 11, 2007 Im trying it 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;"; and it doesn't seem to work?? Quote Link to comment https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321330 Share on other sites More sharing options...
r00tk1LL Posted August 11, 2007 Author Share Posted August 11, 2007 I also tried to do this: 'CREATE TABLE "$var"'; but no success.. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321334 Share on other sites More sharing options...
Fadion Posted August 12, 2007 Share Posted August 12, 2007 A mysql query is constructed this way: $var = 'yourTable' $query = "CREATE TABLE $var"; $results = @mysql_query($query) or die(mysql_error()); If it isnt working, then ure not using mysql_query() (???) or uve got something in assigning your $var. Quote Link to comment https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321342 Share on other sites More sharing options...
r00tk1LL Posted August 12, 2007 Author Share Posted August 12, 2007 Is there a way to see what error this is giving me? My variable is $uname and print $uname; displays the right value, I just cant see what the problem is with sql. Quote Link to comment https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321346 Share on other sites More sharing options...
r00tk1LL Posted August 12, 2007 Author Share Posted August 12, 2007 OK here's what I discovered if I run this with single quotes: $table = 'CREATE TABLE `$uname` (' . ' `id` INT(15) NOT NULL AUTO_INCREMENT PRIMARY KEY, ' . ' `pics` TEXT NOT NULL, ' . ' `thumbs` TEXT NOT NULL' . ' )' . ' ENGINE = myisam;'; It makes the table, but calls is $uname, the variables name instead of value. If i run this with double quotes: $table = "CREATE TABLE `$uname` (' . ' `id` INT(15) NOT NULL AUTO_INCREMENT PRIMARY KEY, ' . ' `pics` TEXT NOT NULL, ' . ' `thumbs` TEXT NOT NULL' . ' )' . ' ENGINE = myisam;"; It finds the variables value, but fails to make the table! PLEASE HELP Quote Link to comment https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321362 Share on other sites More sharing options...
bruckerrlb Posted August 12, 2007 Share Posted August 12, 2007 $table = "CREATE TABLE `$uname` (' . ' `id` INT(15) NOT NULL AUTO_INCREMENT PRIMARY KEY, ' . ' `pics` TEXT NOT NULL, ' . ' `thumbs` TEXT NOT NULL' . ' )' . ' ENGINE = myisam;"; $results = @mysql_query($table) or die(mysql_error()); I think this would work Quote Link to comment https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321371 Share on other sites More sharing options...
r00tk1LL Posted August 12, 2007 Author Share Posted August 12, 2007 it says: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' . ' `id` INT(15) NOT NULL AUTO_INCREMENT PRIMARY KEY, ' . ' `p' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321373 Share on other sites More sharing options...
r00tk1LL Posted August 12, 2007 Author Share Posted August 12, 2007 So the problem looks like MYSQL wants the create table in single quotes, but to get the variable value it has to be in double quotes, how can I get around this? Quote Link to comment https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321378 Share on other sites More sharing options...
bruckerrlb Posted August 12, 2007 Share Posted August 12, 2007 $table = "CREATE TABLE '$uname' ( 'id' INT(15) NOT NULL AUTO_INCREMENT PRIMARY KEY, 'pics' TEXT NOT NULL, 'thumbs' TEXT NOT NULL )' ENGINE = myisam;"; $results = @mysql_query($table) or die(mysql_error()); I think this may work better, I usually use phpmyadmin to create tables, but I know this option is not always available Quote Link to comment https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321379 Share on other sites More sharing options...
r00tk1LL Posted August 12, 2007 Author Share Posted August 12, 2007 It says: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' . ' `id` INT(15) NOT NULL AUTO_INCREMENT PRIMARY KEY, ' . ' `p' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321381 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.