Reputation: 43
I'm trying to create a table if it does not already exist in my database. For this I'm running this test which is working as intended:
$conn = mysql_connect("localhost", "twa222", "twa222bg");
mysql_select_db("airline222", $conn) or die ("Database not found " . mysql_error() );
$val = mysql_query("SELECT 1 from '$FLIGHTID'");
However my problem comes when I try to create the table itself, which is giving me the following error:
Problem with query: 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 ''passenger' SMALLINT NOT NULL, 'booking' CHAR(6), 'seat' VARCHAR(3))' at line 2
This is the code that is attempting to generate the table
if(!$val)
{
$sql = "CREATE TABLE ".$FLIGHTID." (
passenger SMALLINT NOT NULL PRIMARY KEY,
booking CHAR(6), seat VARCHAR(3) )";
$rs = mysql_query($sql) or die ("Problem with query" . mysql_error());
}
mysql_close($conn);
I originally thought it was the ".$FLIGHTID." that was causing the problem but when I changed that to simply be ABC I still got the same error.
Can anyone see where I am going wrong?
EDIT: My SQL output when using ABC is:
CREATE TABLE ABC ( passenger SMALLINT NOT NULL PRIMARY KEY, booking CHAR(6), seat VARCHAR(3) )
Without using ABC it is:
CREATE TABLE ( passenger SMALLINT NOT NULL PRIMARY KEY, booking CHAR(6), seat VARCHAR(3) )
Upvotes: 4
Views: 71
Reputation: 69440
You use single quotes arround column names what is not allowed. Single qoutes indicates that the value inside is a litaral:
Change:
$val = mysql_query("SELECT 1 from '$FLIGHTID'");
to:
$val = mysql_query("SELECT 1 from $FLIGHTID");
Use mysqli_*
or PDO
instead of deprecated mysql_*
API.
Upvotes: 4