Mysql编写的语句 - $ mysqli-> num_rows()返回0

时间:2014-02-02 03:00:58

标签: php mysqli prepared-statement

以下是我遇到问题的代码:

function getQuestions($mysqli, $subjectIdOrCode, $isStudent){                                                                                                     

    $idSubject = getSubjectId($mysqli, $subjectIdOrCode);                                                                                                         

    //writing the statement                                                                                                                                       
    $query = "select id,description from questions where id_subjects = ? and  
              is_for_student = ?";    

    //prepare statement                                                                                                                          
    $stmt = $mysqli->prepare($query);

    //binding the statement                                                                                                                                       
    $stmt->bind_param("si", $idSubject, $isStudent);                                                                                                              

    //execute the statement
    $stmt->execute();   

    //get the result  
    $result = $stmt->get_result();  

    //store the result                                                                                                                                            
    $stmt->store_result();     

    //get the number of rows                                                                                                                                      
    $noOfRows = $stmt->num_rows();  

    $questions = null;                                                                                                                                            
    for ($i = 0; $i < $noOfRows; $i++) {                                                                                                                          
        echo "test";
        $row = $result->fetch_array(MYSQLI_ASSOC);
        $questions[$i]['id'] = $row['id'];
        $questions[$i]['sno'] = $i+1;
        $questions[$i]['description'] = $row['description'];
    }

    return $questions;

}

调用此函数时,不会打印任何内容(这意味着$noOfRows为0)。现在,当行:

    //get the result
    $result = $stmt->get_result();

已删除,它会打印test以及$result未定义的一些错误消息(这清楚地表明$noOfRows是&gt; 0)。

我的代码在哪里犯了错误?

提前致谢!

1 个答案:

答案 0 :(得分:1)

好的......我明白了。我刚刚把代码放到了获取num_rows

    //get the number of rows                                                                                                                                      
    $noOfRows = $stmt->num_rows;