收到此错误但不确定发生了什么。使用mysqli的新手。
$query = 'SELECT name, email FROM US__users WHERE state="' . $state . '" ORDER BY zip ASC LIMIT 5';
$result = $mysqli->query($query);
while ($row = $result->fetch_array()) {
print ($row['name'] . ' ' . $row['email']);
}
$result->free();
目前MySQL DB中只有一行。但是,当我复制$ query并在mysql命令提示符下运行它时,它会查询并显示一个结果。
答案 0 :(得分:2)
查询字符串在语法上是不正确的。你有这个:
$query = 'SELECT name, email FROM users WHERE state="' $state '" ORDER BY zip ASC LIMIT 5';
请尝试使用允许字符串替换的双引号("
):
$query = "SELECT name, email FROM users WHERE state='$state' ORDER BY zip ASC LIMIT 5";
或者,如果您因某种原因需要串联值:
$query = "SELECT name, email FROM users WHERE state='" . $state . "' ORDER BY zip ASC LIMIT 5";
如果您仍然遇到问题,另一个想法是使用预备语句方法;你永远不知道是否需要过滤state
。试试吧。
$query = "SELECT name, email FROM US__users WHERE state=? ORDER BY zip ASC LIMIT 5";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('s', $state);
$result = $stmt->execute();
$stmt->close();
while ($row = $result->fetch_array()) {
print ($row['name'] . ' ' . $row['email']);
}
$result->free();
答案 1 :(得分:0)
您的查询定义缺少连接:
$query = 'SELECT name, email FROM users WHERE state="' . $state . '" ORDER BY zip ASC LIMIT 5';
请查看以下链接以获取更多信息: call to a member function non-object
答案 2 :(得分:-1)
查询因
而中断state="' $state '"
尝试
$query = "SELECT name, email FROM
users WHERE state='$state' ORDER BY zip ASC LIMIT 5";
答案 3 :(得分:-1)
这可能是一个PHP错误?刚刚面临同样的问题。所以它并不像你想要的那样工作(对于未来的人,喜欢我的人会寻找答案):while ($row = $result->fetch_array()) {
。但由于某种原因,它的工作方式(看起来很丑陋)
$query = 'SELECT name, email FROM US__users WHERE state="' . $state . '" ORDER BY zip ASC LIMIT 5';
while ($row = $mysqli->fetch_array($query)) {...