mysqli绑定参数错误

时间:2017-07-18 16:11:42

标签: php mysqli

if ($stmt = $mysqli->prepare("SELECT x, y FROM users WHERE x > ( ?  - 10) AND x< ( ? + 10 ) AND y> ( ? - 10 ) AND y<( ? + 10 ) ")){

            $stmt->bind_param('ii', $x , $y); 
            $stmt->execute();

如何在不收到错误的情况下使此代码正常工作

  

PHP警告:mysqli_stmt :: bind_param():变量数与预编译语句中的参数数量不匹配...

我是这样的,它实际上有效:($stmt = $mysqli->prepare("SELECT x, y FROM users WHERE x>$x-10 AND x<$x+10 AND y>$y-10 AND y<$y+10 ") 但我得到了相同的“变量数量与参数数量不匹配”错误,因为我当然没有真正使用任何绑定参数。

2 个答案:

答案 0 :(得分:3)

解决方案(使用一些代码重构作为奖励:))。

$stmt = $mysqli->prepare("SELECT `x`, `y` FROM `users` WHERE `x` > ( ?  - 10) AND `x` < ( ? + 10 ) AND `y` > ( ? - 10 ) AND `y` <( ? + 10 )");
if( $stmt )
{
    $stmt->bind_param('iiii', $x , $x, $y , $y); 
    $stmt->execute();
}

答案 1 :(得分:2)

您必须绑定所有参数。我建议你这样做:

if ($stmt = $mysqli->prepare("SELECT x, y FROM users WHERE x > (?-10) AND x < (?+10) AND y > (?-10) AND y < (?+10)")) {
    $stmt->bind_param('iiii', $x, $x , $y, $y); 
    $stmt->execute();