PHP return 更改 Table 添加主键时出错
PHP return error when Alter Table Add Primary Key
10.1.15-MariaDB,PHP5.4
使用的代码:
$table = 'abc';
mysql_query("ALTER TABLE `$table` ADD PRIMARY KEY (`col`)");
错误:
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 \'ALTER TABLE...
我试过 KEY `PRIMARY` 并删除所有反引号都无济于事。
Updates:
It has nothing to do with extensions whatsoever, tested with mysqli_ same error. However, repeated test using same script in different server environment is fine.
我试过这段代码,它对我有用
$table = 'abc';
$query="ALTER TABLE `".$table."` ADD PRIMARY KEY(`col`);";
/* Execute query */
$result=$mysqli->query($query);
/* Verify results */
if(!$result) {
$ErrMessage = "Error : " $mysqli->error . "\n";
$mysqli->close();
die($ErrMessage);
}
您是否有权修改 table?
这里是与 mysqli 的快速连接
$mysqli = new mysqli($db_host, $db_user, $db_pass,$database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die ("<h1>can't use database !</h1>");
exit();
}
/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
printf("error loding 'character set utf8' : %s\n", $mysqli->error);
}
首先感谢大家的帮助。将脚本复制并粘贴到显示大量错误描述的不同服务器环境后,我得到了答案。
right syntax to use near '\xef\xbb\xbfALTER TABLE abc
ADD PRIMARY KEY
恶魔是\xef\xbb\xb
发生这种情况是因为我必须处理多种语言。不过,我确实希望能给我一些建议,让我以后如何避免这些愚蠢的错误。
10.1.15-MariaDB,PHP5.4
使用的代码:
$table = 'abc';
mysql_query("ALTER TABLE `$table` ADD PRIMARY KEY (`col`)");
错误:
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 \'ALTER TABLE...
我试过 KEY `PRIMARY` 并删除所有反引号都无济于事。
Updates: It has nothing to do with extensions whatsoever, tested with mysqli_ same error. However, repeated test using same script in different server environment is fine.
我试过这段代码,它对我有用
$table = 'abc';
$query="ALTER TABLE `".$table."` ADD PRIMARY KEY(`col`);";
/* Execute query */
$result=$mysqli->query($query);
/* Verify results */
if(!$result) {
$ErrMessage = "Error : " $mysqli->error . "\n";
$mysqli->close();
die($ErrMessage);
}
您是否有权修改 table?
这里是与 mysqli 的快速连接
$mysqli = new mysqli($db_host, $db_user, $db_pass,$database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die ("<h1>can't use database !</h1>");
exit();
}
/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
printf("error loding 'character set utf8' : %s\n", $mysqli->error);
}
首先感谢大家的帮助。将脚本复制并粘贴到显示大量错误描述的不同服务器环境后,我得到了答案。
right syntax to use near '\xef\xbb\xbfALTER TABLE
abc
ADD PRIMARY KEY
恶魔是\xef\xbb\xb
发生这种情况是因为我必须处理多种语言。不过,我确实希望能给我一些建议,让我以后如何避免这些愚蠢的错误。