SQL Query with arithmetic

时间:2013-12-10 15:33:03

标签: php sql mysqli

我正在处理查询并收到此错误。

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\getAssets.php on line 19

我知道我的查询错误,我无法弄清楚原因。这可能是愚蠢的事情,我一直在看它太久了。

$result = mysqli_query($con,'SELECT * FROM location WHERE ((320000 * ABS(SQRT(SQUARE(xCoor - "' . $_POST['latitude'] .'" ) + SQUARE(yCoor - "' . $_POST['longitude'] .'" ))) < 50)');

非常感谢你的第二双眼睛!

1 个答案:

答案 0 :(得分:0)

您的查询语法错误:您应该像这样编写它(不需要单引号):

$rs = mysqli_query($con, "SELECT * FROM location WHERE ((320000 * ABS(SQRT(SQUARE(xCoor - " . $_POST['latitude'] . ") + SQUARE(yCoor - " . $_POST['longitude'] . " ))) < 50)");

当然$ _POST ['纬度']和$ _POST ['经度'] 必须是数字......

你还应该添加你正在使用的mysqli_fetch_array代码,因为你提供的代码不会产生这样的错误......: - )

作为一般规则,$ result(结果集)应该传递给mysqli_fetch_array()......

$rs = mysqli_query($con, "SELECT * FROM location WHERE ((320000 * ABS(SQRT(SQUARE(xCoor - " . $_POST['latitude'] . ") + SQUARE(yCoor - " . $_POST['longitude'] . " ))) < 50)");
if (!$rs) {
    die(mysqli_error());
}

while ($row = mysqli_fetch_array($rs, MYSQLI_BOTH)) {
     $field1 = $row["field1"];
     ...
}