[英]How to store results of multiple queries in different array variables
I need to get data from 2 different queries. 我需要从2个不同的查询中获取数据。 I want to store them as 2 separate variables.
我想将它们存储为2个单独的变量。
First I tried this: 首先,我尝试了这个:
// 1. Create a database connection
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME, DB_PORT);
if ($mysqli->connect_errno) {
echo "No connection" ;
}
// 2. first query
$query = "CALL sp1('2015-01-01', '2015-01-31');";
$result = $mysqli->query($query);
$var1 = array();
while($shop = $result->fetch_object()) {
$var1[] = $shop;
}
echo '<pre>', print_r($var1), '</pre>';
$result->free();
// 3. second query
$query = "CALL sp2('2015-01-01', '2015-01-31');";
$result = $mysqli->query($query);
$var2 = array();
while($shop = $result->fetch_object()) {
$var2[] = $shop;
}
echo '<pre>', print_r($var2), '</pre>';
I get the data from 1st query however when php tries to fetch 2nd query I got this error: 我从第一个查询获取数据,但是当php尝试获取第二个查询时,出现此错误:
Fatal error: Call to a member function fetch_object() on boolean
致命错误:在布尔值上调用成员函数fetch_object()
I read about multi_query but couldn't find anything about saving output from queries as separate variables. 我读到有关multi_query的信息,但找不到有关将查询的输出另存为单独变量的任何信息。
Additional info: Even when I switch queries always the second one is not correct. 附加信息:即使我总是切换查询,第二个查询也不正确。 I don't know how to load second query and what is happening to the previous one (gets deleted when I write:result->free?)
我不知道如何加载第二个查询以及上一个查询发生了什么(我写:result-> free时被删除了)
The solution was this little line: 解决的方法是:
$results->free();
// solution
$mysqli->next_result();
It seems that mysqli needs information that one query is finished. 似乎mysqli需要一个查询已完成的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.