-1

我想使用准备好的语句查询我的 MySQL 数据库。查询应如下所示:

SELECT name, idname FROM names WHERE origin='english' AND name like('%a%')"

这个效果很好:

$origin = "english";
$stmt = $mysqli->prepare("SELECT name, idname FROM names WHERE origin=? AND name like('%a%')"))
$stmt->bind_param("s", $origin);

但这根本不起作用(即使没有错误):

$origin = "english";
$letter = "a";
$stmt = $mysqli->prepare("SELECT name, idname FROM names WHERE origin=? AND name like(%?%)")) 
$stmt->bind_param("ss", $origin, $letter);

请注意最后一个学期like。我不知道如何绑定第二个参数letter

4

1 回答 1

1

使用语句将值传递给LIKE谓词CONCAT

修正版:

$origin = "english";
$letter = "a";
$stmt = $mysqli->prepare("SELECT name, idname FROM names WHERE origin=? 
AND name LIKE CONCAT('%',?,'%')")) 
$stmt->bind_param("ss", $origin, $letter);
于 2012-05-06T14:49:23.030 回答