SELECT DISTINCT in mysqli prepared statement?

2019-09-20 11:37发布

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;
    }

标签: php mysqli
1条回答
Summer. ? 凉城
2楼-- · 2019-09-20 12:16

is there any difference between select distinct in mysql function and mysqli prepared statement?

Obviously, no.
API syntax has absolutely nothing to do with SQL syntax.

Your problem is somewhere else. You need to learn to debug your application to make yourself aware of the real cause and eventually be able to post a question on the real problem you have, instead of blindly poking by chance. You may start here: https://stackoverflow.com/a/22662582/285587

查看更多
登录 后发表回答