【发布时间】: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