我遇到了mysql条件注释查询的问题,其中报告错误而没有语法错误.它适用于至少有一个查询实际上是以条件执行的情况.
我使用的是php 5.6.24和mysql 5.5.52-cll
示例1(成功):
<?php
$conn = mysqli_connect("127.0.0.1", "aaatex_phppos", "phppos", "aaatex_phppos2");
$test1 = "
/*!40000 REPLACE INTO `phppos_app_config` (`key`, `value`) VALUES ('supports_full_text', '0') */;
/*!50604 REPLACE INTO `phppos_app_config` (`key`, `value`) VALUES ('supports_full_text', '1') */;";
mysqli_multi_query($conn,$test1);
print_r(mysqli_error_list($conn));
?>
Run Code Online (Sandbox Code Playgroud)
supports_full_text值为0,如预期的那样.
例2(失败):
<?php
$conn = mysqli_connect("127.0.0.1", "aaatex_phppos", "phppos", "aaatex_phppos2");
$test2 = "
/*!50604 REPLACE INTO `phppos_app_config` (`key`, `value`) VALUES ('test', '0') */;
/*!50604 REPLACE INTO `phppos_app_config` (`key`, `value`) VALUES ('test', '1') */;";
mysqli_multi_query($conn,$test2);
print_r(mysqli_error_list($conn));
Run Code Online (Sandbox Code Playgroud)
收到的错误:
Array
(
[0] => Array
(
[errno] => 1064
[sqlstate] => 42000
[error] => 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 ';
/*!50604 REPLACE INTO `phppos_app_config` (`key`, `value`) VALUES ('test', '1' at line 1
)
)
Run Code Online (Sandbox Code Playgroud)
示例3(失败;但看似成功(见下面的消息):
<?php
$conn = mysqli_connect("127.0.0.1", "aaatex_phppos", "phppos", "aaatex_phppos2");
$test3 = "
/*!40000 REPLACE INTO `phppos_app_config` (`key`, `value`) VALUES ('test', '0') */;
/*!50604 REPLACE INTO `phppos_app_config` (`key`, `value`) VALUES ('test', '0') */;
/*!50604 REPLACE INTO `phppos_app_config` (`key`, `value`) VALUES ('test', '1') */;";
mysqli_multi_query($conn,$test3);
print_r(mysqli_error_list($conn));
Run Code Online (Sandbox Code Playgroud)
测试值为0,如预期.
这是php中的错误还是我做错的事情?
编辑:
注意:我发现当查询失败时,它会STOPS处理文件的其余部分.因此,示例3在第2和第3个查询中仍然存在错误; 我只是没有发现所有错误.40000查询有效; 但是任何不为当前mysql版本运行的都会因语法错误而失败.
你在这里有误解.你的mysql版本是5.5.52.这意味着您获得的结果是正确的.
当你/*!40000 ... */在查询中说,你说这个查询应该只在高于4.0.0的mysql版本中执行.同样,意味着mysql版本应该高于此查询执行.请记住,这些数字与mysql版本有关.不是php版本./*!50604 ... */5.6.04
在你的第一个测试中,由于你的mysql版本大于4.0.0,第一个查询被执行得很好.但是由于你的mysql版本低于5.6.04,所以跳过了第二个查询.这也是其他两个测试中发生的情况.
但是我无法解释你为什么会遇到语法错误,
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 ';
/*!50604 REPLACE INTO `phppos_app_config` (`key`, `value`) VALUES ('test', '1' at line 1
Run Code Online (Sandbox Code Playgroud)
在第二次测试中.可能是您在此处放置的查询不是您执行的实际查询.你能检查一下吗?因为我也做了所有这些测试(我也有mysql 5.5和php 5.6),我没有遇到任何错误.我只看到没有执行更高版本要求的查询.
有关其他阅读,请参阅本文.希望我的回答能帮到你.
更新
通过查看其他答案,您似乎面临着一个罕见的错误.尝试更新您的mysql版本.如果问题仍然存在,则可能是mysql API的错误.