I am trying to convert a mysql function into mysqli prepared statement.
however, I have problems getting it to work especially the select distinct.
is there any difference between select distinct in mysql function and mysqli prepared statement?
This works:
$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);
This Doesn't work:
$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);
am I missing anything in my prepared statement code? and what do I need to do to make it work?
Thanks in advance.
EDIT: This works as well but it is not prepared statement:
$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;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…