PHP STMT MySQL查询在设置为等于时返回相似的结果
也许我遗漏了一些明显的东西,但我没有从这个查询中得到预期的结果。我从一个非严格的查询中得到了我所期望的结果PHP STMT MySQL查询在设置为等于时返回相似的结果,php,mysql,Php,Mysql,也许我遗漏了一些明显的东西,但我没有从这个查询中得到预期的结果。我从一个非严格的查询中得到了我所期望的结果 public function get_product_list_by_title($title){ $result = ARRAY(); $stmt = $this->cn->prepare("SELECT `id`, `name`, `price`, `comments` FROM Products WHERE `name` = ?"); $stmt
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”
将其视为字符串。什么类型的列是name
?您可能正在将整数与字符串进行比较,这将导致隐式转换。谢谢您的帮助!这是一个瓦查尔被比作一根绳子。谢谢你,我只是看不见那根绳子的树木,不敢相信我错过了它!