也许我遗漏了一些明显的东西,但我没有从这个查询中得到预期的结果。我从一个非严格的LIKE查询中得到了我所期望的结果。
public function get_product_list_by_title($title){
$result = ARRAY();
$stmt = $this->cn->prepare("SELECT `id`, `name`, `price`, `comments` FROM Products WHERE `name` = ?");
$stmt->bind_param("i", $title);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result(
$id,
$name,
$price,
$notes
);
while ($stmt->fetch()) {
$result[] = Array(
"id" => $id,
"name" => $name,
"price" => $price,
"notes" => $notes
);
}
$stmt->free_result();
$stmt->close();
return $result;
}
例如,如果我搜索"3",我会得到每个项目的开头都有3(可能更多——这是一个小数据集)
有什么想法吗?
根据您的注释,$title
是字符串,name
列是VARCHAR
。但当你像这个一样绑定$title
时
$stmt->bind_param("i", $title);
强制将其转换为整数。将该行更改为
$stmt->bind_param("s", $title);
在bind_param
中,"i"
表示参数以整数形式传递,而"s"
则将其视为字符串。