0

Using a PDO class with Mysqli I get unexpected results. Check this comparison that should produce the same results imho.

$versions is an array containing elements 8, 9 or both. In both examples below, the array is:

Array
    (
        [0] => 9
        [1] => 8
    )

Using this (more correctly) PDO method:

function getQuestions($nrOfQuestions = 25, $versions){
    $versions = implode(",", $versions);
    $args= array(
            "nrOfQ" => $nrOfQuestions
            ,"versions" => $versions
    );
    return $db->query("SELECT * FROM `" . TABLE . "` WHERE `version` IN (:versions) && `qc` >= 1 ORDER BY RAND() LIMIT :nrOfQ", $args);
}

Unexpected Result: all results are version 9 (I expect 8 aswel here)


Using this (dirty?) concatenation method:

function getQuestions($nrOfQuestions = 25, $versions){
    $versions = implode(",", $versions);
    $args= array(
            "nrOfQ" => $nrOfQuestions
    );
    return $db->query("SELECT * FROM `" . TABLE . "` WHERE `version` IN (" . $versions . ") && `qc` >= 1 ORDER BY RAND() LIMIT :nrOfQ", $args);
}

Result is as expected : Results are randomly version 8 and 9 as expected.


My question is: Why the difference here in the resultset? The IN clause in the query should be (9,8) in both cases but produce different results.

Cagy79
  • 1,610
  • 1
  • 19
  • 25

0 Answers0