mysqli prepare语句出错

时间:2014-08-01 07:22:42

标签: php mysql mysqli prepared-statement

$client = $db -> prepare('SELECT CLIENT_ID, CLIENT_NAME FROM client WHERE CLIENT_NAME LIKE "%?%"');
    $query = $_GET['query'];
    $client -> bind_param('s', $query);
    $client -> execute();
    $client -> bind_result($client_id, $client_name);
    while($client -> fetch()){
        $d['suggestions']['value'] = $client_name;
        $d['suggestions']['data'] = $client_id;
    }
    $client -> free_result();

我在上述声明中收到以下错误。看不出是什么导致了错误。

<b>Warning</b>:  mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in <b>getclient.php</b> on line <b>15</b><br />
<br />
<b>Notice</b>:  Undefined index: callback in <b>getclient.php</b> on line <b>24</b>

现在我有另一条错误消息:

Call to a member function bind_param() on a non-object

1 个答案:

答案 0 :(得分:2)

关于百分比,你不能像那样做LIKE语句。

$client = $db -> prepare('SELECT CLIENT_ID, CLIENT_NAME FROM client WHERE CLIENT_NAME LIKE ?');
$query = $_GET['query'];
$client -> bind_param('s', "%{$query}%");
$client -> execute();
$client -> bind_result($client_id, $client_name);
while($client -> fetch()){
    $d['suggestions']['value'] = $client_name;
    $d['suggestions']['data'] = $client_id;
}
$client -> free_result();`