Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

php - SELECT DISTINCT in mysqli prepared statement?

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...