0

I need to get data from 2 different queries. I want to store them as 2 separate variables.

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:

Fatal error: Call to a member function fetch_object() on boolean

I read about multi_query but couldn't find anything about saving output from queries as separate variables.

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?)

Mike Gorski
  • 117
  • 2
  • 13
  • 1
    I think your query failed and return `false` to `$result`. check `$result` after second query with `var_dump` and make sure your query run correctly – Saeed M. Feb 10 '16 at 23:05
  • When using 2+ different sql statements I just nest while statements. – a coder Feb 10 '16 at 23:11
  • Possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource (or mysqli\_result), boolean given](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result-boole) – Don't Panic Feb 10 '16 at 23:37
  • After applying var_dump on second $result I got false... but why? – Mike Gorski Feb 11 '16 at 18:50

1 Answers1

0

The solution was this little line:

$results->free();
// solution
$mysqli->next_result();

It seems that mysqli needs information that one query is finished.

Mike Gorski
  • 117
  • 2
  • 13