Page 1 of 1

Create Table with Variable name

Posted: Tue Dec 07, 2004 1:47 pm
by rmul1966
can someone show me or explain to me why when I run the following script, it creates the table as the Variable name not the contents of the variable
I have tried putting quotes around the variable so that it is "$TB_Name" I tried .$TB_Name. did not work
If I substitute the Variable with a name such as DORLIST
the table gets created correctly

Code: Select all

<?php
<?php				
// I echo the Variable name out to make sure it is correct 
echo "$TB_Name <br />";
include $_SERVER['DOCUMENT_ROOT']."/php/connectdor.php";
$sql = 'CREATE TABLE $TB_Name (
id int(6) NOT NULL auto_increment,
Title varchar(5) NOT NULL,
Last_Name varchar(25) NOT NULL,
First_Name varchar(15) NOT NULL,
Middle_Name varchar(10) NOT NULL,
DFD DATETIME NOT NULL,
DOB DATETIME NOT NULL,
DOD DATETIME NOT NULL, 
Spouse varchar(15) NOT NULL,
Addr1 varchar(30) NOT NULL,
City varchar(20) NOT NULL,
State varchar(2) NOT NULL,
ZIP varchar(5) NOT NULL,
Phone varchar(20) NOT NULL,
PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id) )'; 
			 
echo 'Creating table: $TB_Name...............';
mysql_query( $sql );

mysql_close($dbh);
?>
?>

Posted: Tue Dec 07, 2004 2:08 pm
by potsed
change the lines in the sql var

Code: Select all

$sql = 'CREATE TABLE $TB_Name (...';
// and the echo statement...
echo 'Creating table: $TB_Name...............';
to

Code: Select all

$sql = 'CREATE TABLE '.$TB_Name.' (...';
// and ...
echo 'Creating table: '.$TB_Name.'..............';
And read the manual for the difference between single and double quotes...

Posted: Tue Dec 07, 2004 2:26 pm
by rmul1966
Thank you very much I tried many different variations of the quotes and did not realize that was the issue.

I will read about the quotes in manual .
Thank you for your assistance

Posted: Tue Dec 07, 2004 2:43 pm
by josh
To sum up the quotes issue

Code: Select all

$test='$var test'; // doesnt work
$test = $var . ' test'; //does work
$test = "$var test"; // does work