Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
193 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...