为什么创建 table 然后插入多个值 return 会出错?
Why does creating a table and then inserting multiple values return an error?
所以我正在尝试创建一个 table,然后向其中插入多个值,如下所示:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "someDbName";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "CREATE TABLE IF NOT EXISTS someTableName(
someID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
someVar1 VARCHAR(30) NOT NULL,
someVar2 INT NOT NULL);
INSERT INTO someTableName (someVar1 , someVar2 ) VALUES ('someString1', someInteger1),
('someString2',someInteger2);";
其中 someInteger 位当然是整数。然后:
$sql = mysqli_real_escape_string($conn, $sql);
if (mysqli_multi_query($conn, $sql)) {
dtconsole("Tables populated successfully");
} else {
dtconsole("Error creating table: " . mysqli_error($conn));
}
有了 dtconsole 功能,就可以输出到控制台来帮助我调试。
function dtconsole($data){
$output=$data;
if(is_array($output)){
$output=implode(',',$output);
}
echo '<script>console.log("'.$output.'");</script>';
}
每次我尝试运行这个,它returns以下错误:
Error creating table: You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near '
someID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, ' at line 1
我这辈子都看不出自己做错了什么。
您的问题是您在整个查询中调用 mysqli_real_escape_string
,而不仅仅是您插入的值。结果,它将 $sql 字符串中的 CR-LF 转换为 \r\n
,MySQL 解析器无法解释。你应该这样做:
$someString1 = mysqli_real_escape_string($conn, 'someString1');
$someString2 = mysqli_real_escape_string($conn, 'someString2');
$sql = "CREATE TABLE IF NOT EXISTS someTableName(
someID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
someVar1 VARCHAR(30) NOT NULL,
someVar2 INT NOT NULL);
INSERT INTO someTableName (someVar1 , someVar2 ) VALUES ($someString1, someInteger1),
($someString2,someInteger2);";
if (mysqli_multi_query($conn, $sql)) {
...
所以我正在尝试创建一个 table,然后向其中插入多个值,如下所示:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "someDbName";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "CREATE TABLE IF NOT EXISTS someTableName(
someID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
someVar1 VARCHAR(30) NOT NULL,
someVar2 INT NOT NULL);
INSERT INTO someTableName (someVar1 , someVar2 ) VALUES ('someString1', someInteger1),
('someString2',someInteger2);";
其中 someInteger 位当然是整数。然后:
$sql = mysqli_real_escape_string($conn, $sql);
if (mysqli_multi_query($conn, $sql)) {
dtconsole("Tables populated successfully");
} else {
dtconsole("Error creating table: " . mysqli_error($conn));
}
有了 dtconsole 功能,就可以输出到控制台来帮助我调试。
function dtconsole($data){
$output=$data;
if(is_array($output)){
$output=implode(',',$output);
}
echo '<script>console.log("'.$output.'");</script>';
}
每次我尝试运行这个,它returns以下错误:
Error creating table: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' someID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, ' at line 1
我这辈子都看不出自己做错了什么。
您的问题是您在整个查询中调用 mysqli_real_escape_string
,而不仅仅是您插入的值。结果,它将 $sql 字符串中的 CR-LF 转换为 \r\n
,MySQL 解析器无法解释。你应该这样做:
$someString1 = mysqli_real_escape_string($conn, 'someString1');
$someString2 = mysqli_real_escape_string($conn, 'someString2');
$sql = "CREATE TABLE IF NOT EXISTS someTableName(
someID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
someVar1 VARCHAR(30) NOT NULL,
someVar2 INT NOT NULL);
INSERT INTO someTableName (someVar1 , someVar2 ) VALUES ($someString1, someInteger1),
($someString2,someInteger2);";
if (mysqli_multi_query($conn, $sql)) {
...