所以我试图学习在我的查询中使用预处理语句,因为它使用了旧的mysql方式,但我没有太多运气。
这是我的代码
<?php
$stmt = $db->stmt_init();
if($stmt->prepare("SELECT l.listingID, l.title, l.description, l.dateListed
FROM tbl_listings AS l
LEFT JOIN tbl_listing_type AS lt ON lt.listingID = l.listingID
LEFT JOIN tbl_type AS t ON t.typeID = lt.typeID
WHERE lt.typeID =?"))
{
$stmt->bind_param("i",$type);
$type = 1;
$stmt->bind_result($id, $title, $description, $date);
while($stmt->fetch())
{
echo $id . ' - ' . $title . ' - ' . $description . ' - '.$date."<br />";
}
$stmt->close();
}
else
{
echo "error";
}
但它不打印任何东西,我在我的phpmyadmin中运行查询而不是?它返回记录,所以我知道查询是正确的,但我不确定我正确使用准备好的位?有人可以告诉我我可能出错的地方吗?
非常感谢答案 0 :(得分:1)
您需要在绑定之前将值分配给$type
:
$type = 1;
$stmt->bind_param("i",$type);
答案 1 :(得分:1)
$stmt = $db->stmt_init();
if($stmt->prepare("SELECT l.listingID, l.title, l.description, l.dateListed
FROM tbl_listings AS l
LEFT JOIN tbl_listing_type AS lt ON lt.listingID = l.listingID
LEFT JOIN tbl_type AS t ON t.typeID = lt.typeID
WHERE lt.typeID =?"))
{
$type = 1;
$stmt->bind_param("i",$type);
$stmt->execute(); <-- Was missing this in my original code
$stmt->bind_result($id, $title, $description, $date);
while($stmt->fetch())
{
echo $id . ' - ' . $title . ' - ' . $description . ' - '. $date . "<br />";
}
$stmt->close();
}?>
让它工作:)缺少执行查询所需的一行代码。