-1

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

1 Answers1

-3

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

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Cheers, I have all that in my php file and I don't get any error(s). – user3585872 Apr 30 '14 at 12:23
  • Why post an answer if it (as you claim) is not answerable? – Qantas 94 Heavy May 11 '14 at 02:12
  • @Qantas94Heavy it is answerable all right, and answered. If you can't see the answer, better quit that review business. – Your Common Sense May 11 '14 at 06:06
  • What the hell? You *literally* said "You need too learn to debug your application to make yourself aware of the real cause and eventually be able to post an answerable question on Stack Overflow", implying that the question is not answerable. Don't give me bullshit. – Qantas 94 Heavy May 11 '14 at 09:29
  • @Qantas94Heavy that's *another* question, that was only *implied* by the OP. His real question is "Why doesn't my code work", while he preferred to ask a pointless one if there is any difference between two drivers. Again: if you can't read and comprehend the question - better quit that review business. – Your Common Sense May 11 '14 at 09:41