我正在构建一个小框架来动态查询数据库并返回该查询的所有结果集。为了使这个方法起作用,我必须处理返回多个结果集的过程,所以我将基于next_result()条件为true来放置一段时间。下面的代码工作正常,但是当我取消注释第二个fetch()时,var_dump next_result()总是返回true。我认为这是某种bug或其他什么。但正因为如此,我无法管理此代码。
任何提示?任何解决方法?
public function query($database, $method, $parameters) {
$database -> connect();
$sql = "CALL" . " " . $method . "(" . (count($parameters) === 0 ? "" : (str_repeat("?, ", count($parameters) - 2) . "?")) . ");";
$stmt = $database -> connection -> prepare($sql);
if (count($parameters) > 0) {
call_user_func_array(array($stmt, 'bind_param'), $parameters);
}
$stmt -> execute();
$stmt -> store_result();
// i will have a while here
$i = 0;
$metadata = $stmt -> result_metadata();
$fields = $metadata -> fetch_fields();
$columns = array();
foreach ($fields as $field) {
$j[$field -> name] = "";
$columns[$field -> name] = &$j[$field -> name];
}
call_user_func_array(array($stmt, 'bind_result'), $columns);
$rows[$i] = array();
while ($stmt -> fetch()) {
$row = new stdClass();
foreach ($columns as $key => $value) {
$row -> $key = $value;
}
$rows[$i] = $row;
}
$i++;
var_dump($stmt -> next_result());
$metadata = $stmt -> result_metadata();
$fields = $metadata -> fetch_fields();
$columns = array();
foreach ($fields as $field) {
$j[$field -> name] = "";
$columns[$field -> name] = &$j[$field -> name];
}
call_user_func_array(array($stmt, 'bind_result'), $columns);
$rows[$i] = array();
/*while ($stmt -> fetch()) {
$row = new stdClass();
foreach ($columns as $key => $value) {
$row -> $key = $value;
}
$rows[$i] = $row;
}*/
var_dump($stmt -> next_result());
$stmt -> free_result();
$stmt -> close();
$database -> disconnect();
return $rows;
}
答案 0 :(得分:1)
我发现基于元数据变量的解决方案是假的:
public function query($database, $method, $parameters) {
$database -> connect();
$sql = "CALL" . " " . $method . "(" . (count($parameters) === 0 ? "" : (str_repeat("?, ", count($parameters) - 2) . "?")) . ");";
$stmt = $database -> connection -> prepare($sql);
if (count($parameters) > 0) {
call_user_func_array(array($stmt, 'bind_param'), $parameters);
}
$stmt -> execute();
$stmt -> store_result();
$i = 0;
while ($metadata = $stmt -> result_metadata()) {
$fields = $metadata -> fetch_fields();
$columns = array();
foreach ($fields as $field) {
$j[$field -> name] = "";
$columns[$field -> name] = &$j[$field -> name];
}
call_user_func_array(array($stmt, 'bind_result'), $columns);
$rows[$i] = array();
while ($stmt -> fetch()) {
$row = new stdClass();
foreach ($columns as $key => $value) {
$row -> $key = $value;
}
$rows[$i] = $row;
}
$i++;
$stmt -> next_result();
}
$stmt -> free_result();
$stmt -> close();
$database -> disconnect();
return $rows;
}