【发布时间】:2014-04-30 12:07:26
【问题描述】:
我正在尝试将 mysql 函数转换为 mysqli 准备好的语句。
但是,我无法让它工作,尤其是 select distinct。
mysql函数中的select distinct和mysqliprepared statement有什么区别吗?
这行得通:
$sql = "SELECT DISTINCT category FROM $storeShop";
$result = mysql_query($sql) or die("Query failed : " . mysql_error());
// For each result that we got from the Database
while ($line = mysql_fetch_assoc($result))
{
$cvalue[] = $line;
}
// Assign this array to smarty...
$smarty->assign('category', $cvalue);
// Assign this array to smarty...
$smarty->assign('$category', $cvalue);
这不起作用:
$stmt = mysqli_prepare($db_conx, "SELECT DISTINCT category FROM $storeShop");
$stmt->execute();
$stmt->bind_result($category);
/* fetch values */
while ($line = ($stmt->fetch())) {
$cvalue[] = $line;
}
// Assign this array to smarty...
$smarty->assign('category', $cvalue);
// Assign this array to smarty...
$smarty->assign('$category', $cvalue);
我在准备好的语句代码中是否遗漏了任何内容?我需要做什么才能让它发挥作用?
提前致谢。
编辑:这也可以,但它不是准备好的语句:
$sql = "SELECT DISTINCT category FROM $storeShop";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
while($line = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$cvalue[] = $line;
}
【问题讨论】:
-
顺便说一句,定义“不起作用”
-
@YourCommonSense,我的意思是它只是不返回任何值。它会在我的页面中创建空白空间,就好像它获取值一样,但它只是不打印出值。
-
嗯,现在更清楚了。尝试从您的源中复制/粘贴准备好的语句处理代码没有错误。