【问题标题】:Mysql prepared statements - $mysqli->num_rows() returns 0Mysql 准备好的语句 - $mysqli->num_rows() 返回 0
【发布时间】:2014-02-02 03:00:58
【问题描述】:

这是我遇到问题的代码:

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 > 0)。

我的代码哪里出错了?

提前致谢!

【问题讨论】:

    标签: php mysqli prepared-statement


    【解决方案1】:

    好的……我明白你的意思。我刚刚放了代码来获取 num_rows

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

    【讨论】:

    • 恐怕你误解了我的问题。我想获得面向对象格式的number of rows
    • 好的!最后我想通了。当使用$stmt-&gt;get_result() 时,它的$result-&gt;num_rows 而不是$stmt-&gt;num_rows
    猜你喜欢
    • 2018-02-04
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2011-01-23
    相关资源
    最近更新 更多