4

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

8
  • Seems to work just fine. sqlfiddle.com/#!9/1b0e9 Commented Jun 3, 2015 at 6:14
  • Can you post the generated SQL statement? Commented Jun 3, 2015 at 6:15
  • Is this really your code? Sure you don't use quotes around your column names like 'passenger' instead? Commented Jun 3, 2015 at 6:16
  • 1
    Don't use deprecated mysql_*, use PDO or mysqli_* Commented Jun 3, 2015 at 6:16
  • 1
    What is displayed when you echo $FLIGHTID ? Commented Jun 3, 2015 at 6:18

1 Answer 1

4

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 PDOinstead of deprecated mysql_* API.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.