简体   繁体   English

添加 where 子句时出错

[英]Error when adding where clause

This is likely a beginner error but unfortunately I have not been able to figure it out.这可能是初学者的错误,但不幸的是我无法弄清楚。

When I run the code below I get the error 'errno' => int 1064 'sqlstate' => string '42000' (length=5) 'error' => string 'You have an error in your SQL syntax;当我运行下面的代码时,我收到错误 'errno' => int 1064 'sqlstate' => string '42000' (length=5) 'error' => string '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 '\\'Caribbean\\'' at line 1' (length=159)检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的 '\\'Caribbean\\'' 附近使用的正确语法(长度 = 159)

If I use "select * from Country" the query works but when I add "where Region = 'Caribbean'" it does not.如果我使用“select * from Country”,则查询有效,但是当我添加“where Region = 'Caribbean'”时,它不会。

$sql = "select * from Country where Region = 'Caribbean'";
$db = mysqli_init();
$db->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
@ $db->real_connect('localhost', 'user', 'passwd', 'world');
if ($db->connect_errno) {
    echo "Failed to connect to MySQL: (". $db->connection_errno . ")".
      $mysqli->connect_error;
    exit;  
}
$sql = $db->real_escape_string($sql);
$result = $db->query($sql);
var_dump($db->error_list);
var_dump($result->fetch_all());

You can try to check quote symbols, if you copy from a text file which may be different from the symbol in script.如果您从可能与脚本中的符号不​​同的文本文件复制,您可以尝试检查引号符号。 Also after entering symbols from keyboard, try to put country name in a variable and call by reference, if still getting error.同样在从键盘输入符号后,如果仍然出现错误,请尝试将国家名称放入变量并通过引用调用。

You should escape only the params, not the entire query.您应该只转义参数,而不是整个查询。

something like:类似的东西:

$region = 'Caribbean';
$paramRegion = $db->real_escape_string($region);
$sql = "select * from Country where Region = '".$paramRegion."'";
$db = mysqli_init();
$db->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
@ $db->real_connect('localhost', 'user', 'passwd', 'world');
if ($db->connect_errno) {
    echo "Failed to connect to MySQL: (". $db->connection_errno . ")".
      $mysqli->connect_error;
    exit;  
}
$result = $db->query($sql);
var_dump($db->error_list);
var_dump($result->fetch_all());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM