Zubrag.com :: Forum

Programmers Lounge => PHP / MySql / Smarty Questions => Topic started by: n7qwl on November 10, 2008, 01:54:19 AM



Title: need Help-trying to create a mysql table using a variable as the table name
Post by: n7qwl on November 10, 2008, 01:54:19 AM
I have an html form passing variable information to a php file that will create a mysql table (hopefully) using the value of one of the variables...   The html form has an input called (lastname).

In the php file I am trying to define the variable "$usertable" as $lastname and then... Create a table as follows:

$usertable = (`$lastname`);

$con = mysql_connect($hostname,$username,$password);
if (!$con)
  {
  die('Could not connect ' . mysql_error());
 }// Create table in $dbname database
mysql_select_db($dbname, $con) or die(mysql_error());


$sql = 'CREATE TABLE  `$usertable` ('
        . ' `ID` INT(6) NOT NULL AUTO_INCREMENT, '
        . ' `first` VARCHAR(20) NOT NULL, '
        . ' `middle` VARCHAR(15) NOT NULL, '
        . ' `last` VARCHAR(20) NOT NULL, '
 
and so on...   But the table it is creating is called "$usertable"  NOT the value of the input ("lastname").

Can anyone help me with this?


Title: Re: need Help-trying to create a mysql table using a variable as the table name
Post by: soul on January 26, 2009, 06:53:58 PM
try this

Code:
$usertable = (`.$lastname.`);

$con = mysql_connect($hostname,$username,$password);
if (!$con)
  {
  die('Could not connect ' . mysql_error());
 }// Create table in $dbname database
mysql_select_db($dbname, $con) or die(mysql_error());


$sql = 'CREATE TABLE  `$usertable` ('
        . ' `ID` INT(6) NOT NULL AUTO_INCREMENT, '
        . ' `first` VARCHAR(20) NOT NULL, '
        . ' `middle` VARCHAR(15) NOT NULL, '
        . ' `last` VARCHAR(20) NOT NULL, '

not sure if that will work if not wait for some one else to reply.


Title: Re: need Help-trying to create a mysql table using a variable as the table name
Post by: pipisdicelana on February 05, 2009, 07:17:47 AM
Use double quoted please..  ::)