问题
I am trying to create a table in mySQL. This is my php page below, when I run the page there are no errors but the table is not in mySQL and when I test the code in mySQL I'm getting the error
#1064 - 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 '$user = "root"' at line 1.
I've done a bit of research into what this error means but I'm getting no where. I don't really understand what it means. If I'm honest I don't really understand php I'm just adapting the code I've written in previous uni tutorials. Please help.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
$user="root";
$password="";
$database="test";
mysql_connect('localhost',$user,$password)or die( "Unable to connect to server");
mysql_select_db($database) or die( "Unable to select database");
$query="CREATE TABLE Bookings
(
id int(6) NOT NULL auto_increment,
name varchar(25),
email varchar(35),
number varchar(20),
buffet varchar(3),
ceilidh varchar(5),
work1 varchar(3),
beg1 varchar(3),
int1 varchar(3),
adv1 varchar(3),
youth varchar(3),
lunch varchar(3),
beg2 varchar(3),
int2 varchar(3),
adv2 varchar(3),
dinner varchar(3),
dance varchar(5),
work2 varchar(3),
lunch2 varchar(3),
price varchar(5),
PRIMARY KEY (ID)
)";
mysql_query($query);
mysql_close();
?>
</body>
</html>
回答1:
You need to escape int1
and int2
. They are reserved words in MySQL
CREATE TABLE Bookings
(
id int(6) NOT NULL auto_increment,
name varchar(25),
email varchar(35),
number varchar(20),
buffet varchar(3),
ceilidh varchar(5),
work1 varchar(3),
beg1 varchar(3),
`int1` varchar(3),
adv1 varchar(3),
youth varchar(3),
lunch varchar(3),
beg2 varchar(3),
`int2` varchar(3),
adv2 varchar(3),
dinner varchar(3),
dance varchar(5),
work2 varchar(3),
lunch2 varchar(3),
price varchar(5),
PRIMARY KEY (ID)
)
回答2:
You have problem with your column name(s)
int1s varchar(3),
and
int2 varchar(3),
change that to something else or enclose them in backticks.
Like this...
CREATE TABLE Bookings
(
id int(6) NOT NULL auto_increment,
name varchar(25),
email varchar(35),
number varchar(20),
buffet varchar(3),
ceilidh varchar(5),
work1 varchar(3),
beg1 varchar(3),
`int1` varchar(3), /* see here... */
adv1 varchar(3),
youth varchar(3),
lunch varchar(3),
beg2 varchar(3),
`int2` varchar(3), /* see here... */
adv2 varchar(3),
dinner varchar(3),
dance varchar(5),
work2 varchar(3),
lunch2 varchar(3),
price varchar(5),
PRIMARY KEY (ID)
)
回答3:
You are using reserved words for column names. Check the following link to the mysql reference:
http://dev.mysql.com/doc/refman/5.7/en/reserved-words.html
回答4:
Do not use any reserved words for column names. You need to change the column name int1 and int2 as they are reserved words. For more reserved words please see the link provided by @Alex Vogt.
回答5:
You need to understand the difference between PHP and SQL.
You cannot put everything between <?php
and ?>
into mysql. This is the PHP part. In PHP you will connect to a MYSQL server and send a query to that server. I think you did take everything, because your error specifies near '$user = "root"' at line 1
If you want to test your query, only take the part between $query = "
and ";
and paste that in mysql to test the query.
So that would only be this part:
CREATE TABLE Bookings
(
id int(6) NOT NULL auto_increment,
name varchar(25),
email varchar(35),
number varchar(20),
buffet varchar(3),
ceilidh varchar(5),
work1 varchar(3),
beg1 varchar(3),
int1 varchar(3),
adv1 varchar(3),
youth varchar(3),
lunch varchar(3),
beg2 varchar(3),
int2 varchar(3),
adv2 varchar(3),
dinner varchar(3),
dance varchar(5),
work2 varchar(3),
lunch2 varchar(3),
price varchar(5),
PRIMARY KEY (ID)
)
Once you do that, juergen d's answer might apply: escape int1
and int2
by changing them into `int1`
and `int2`
to escape reserved keywords.
来源:https://stackoverflow.com/questions/20493806/php-create-table-error-1064