Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/64447-create-table-var-help/
Share on other sites

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??

Link to comment
https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321330
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321342
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321362
Share on other sites

$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

Link to comment
https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321371
Share on other sites

$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

Link to comment
https://forums.phpfreaks.com/topic/64447-create-table-var-help/#findComment-321379
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.