以下是我遇到问题的代码:
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)。
我的代码在哪里犯了错误?
提前致谢!
答案 0 :(得分:1)
//get the number of rows
$noOfRows = $stmt->num_rows;