我遇到一个问题,我正在运行一个返回有效结果集的SELECT查询。我使用mysqli_fetch_assoc处理结果,然后将结果集传递给mysqli_free_result,返回false。我在调用之前和之后完成了结果的var_dump,以确认它是一个有效的结果(似乎对我来说 - 我可以访问我期望的数据)。我正在运行的查询类似于:
select us.user_session_key
, u.user_key
, ut.code as user_type
from t_user u
inner join t_user_type ut on u.user_type_key = ut.user_type_key
inner join t_user_session us
on u.user_key = us.user_key
where u.email = 'tim.super@en.com'
and us.session_id = 'mjsvu6fru3dtfk1bbs59fc8t20'
and us.expired_yn = 'N'
order by 1 desc
结果对象的Var转储:
object(mysqli_result)#2 (5) {
["current_field"]=>
int(0)
["field_count"]=>
int(3)
["lengths"]=>
NULL
["num_rows"]=>
int(1)
["type"]=>
int(0)
}
我使用类似于以下代码来处理查询/结果:
$result = mysqli_query ( $conn, $sql );
if ($result) {
var_dump($result);
if (preg_match('/^[[:space:]]*select/i', $sql) == 1) {
while ($row = mysqli_fetch_assoc ( $result )) {
$return_val[sizeof($return_val)] = $row;
}
if (!clear_results($result)) {
print "error: ".$sql;
}
}
} else {
http_response_code(401);
print $sql . "\n";
print mysqli_error($conn);
die("error");
}
function clear_results($result) {
global $conn;
if ($result) {
if (!mysqli_free_result ( $result )) {
return false;
}
}
while (mysqli_more_results($conn)) {
mysqli_next_result($conn);
mysqli_free_result(mysqli_store_result($conn));
}
return true;
}