3

Hope Somebody will help me about how I bind a parameter in mysqli when a multiple character wildcard needs to be next to the variable value. I found that it worked for me when creating a SQL statement, like this:

$sql = "SELECT item_title FROM item WHERE item_title LIKE '%$title%'";

However, I tried to bind the variable following the same pattern, and found that it failed. They used this code:

$sql = "SELECT item_title FROM item WHERE item_title LIKE '%?%'";

It raised this error:

Warning: mysqli_stmt_bind_param() [function.mysqli-stmt-bind-param]: Number of variables doesn't match number of parameters in prepared statement in program_name on line......

Can anybody tell me how fix this problem? Thank you.

4

4 回答 4

5

您只能绑定数据文字而不是任意查询部分。
所以,首先准备你的文字

$var = "%$var%";
$sql = "SELECT item_title FROM item WHERE item_title LIKE ?";
于 2013-02-14T09:56:37.897 回答
2

你可以这样做:

$sql = "SELECT item_title FROM item WHERE item_title LIKE ? ";

接着

$title_new =  '%'.$title.'%';
mysqli_stmt_bind_param($stmt, 's', $title_new);    

根据用户评论更新

实现下面的 SQL

 s2.subject_name LIKE '%$keyword%' OR c.city_name LIKE '%$keyword%' 

在下面使用 MySqli 语句 s2.subject_name LIKE ?或 c.city_name LIKE ?

 $keyword = '%'.$keyword.'%';
 mysqli_stmt_bind_param($stmt, 'ss', $keyword, $keyword);
于 2013-02-14T09:58:47.120 回答
1
   $sql="SELECT item_title FROM item WHERE item_title LIKE concat ('%',?,'%') ";
于 2013-02-22T20:45:00.057 回答
0

每个绑定变量有一个问号。例如 prepare(SELECT item_title FROM item WHERE item_title LIKE ? and name2 like ? and ...) 准备好的语句的好处是您不必担心引用变量。

然后绑定所有参数,如 bind_param("ss...", $param1, $param2, ....);

于 2013-02-22T20:59:07.720 回答