编程语言
首页 > 编程语言> > PHP创建表错误1064

PHP创建表错误1064

作者:互联网

我试图在mySQL中创建一个表.这是我下面的php页面,当我运行页面时没有错误但是表格不在mySQL中,当我在mySQL中测试代码时我得到了错误

#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.

我已经对这个错误意味着什么做了一些研究,但我没有在哪里.我真的不明白这意味着什么.如果我是诚实的我真的不懂php我只是改编我在之前的uni教程中编写的代码.请帮忙.

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

解决方法:

你需要转义int1和int2.他们是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)
)

标签:create-table,php,mysql,syntax-error
来源: https://codeday.me/bug/20191003/1846842.html