1

I'm stuck on this problem for a while now and looking for helpful suggestions.

$resultset = $connection->multi_query($sql);

pseudo:

IF $resultset > 0 (--> if the sql query returned at least one result) {
 // do something
}

now the problem is with the pseudo part, i have tried to the follows:

if($resultset > 0) // takes no effect
if($resultset->num_rows > 0) // Notice: Trying to get property of non-object

I have to use multi_query so mysqli_query/mysqli_fetch_array is not a solution

thanks for the hints....

Riwi
  • 81
  • 11
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Progman Jun 29 '18 at 21:08
  • how should it be a duplicate when the post you referring to, doesn't even contain the expression "multi_query"? in fact it's a link collection of PHP. do you think it's helpful? the reason why someone comes here to ask a problem is not to get answers like "google it". – Riwi Jun 29 '18 at 21:11

2 Answers2

3

Please try:

if($connection->multi_query($sql))
{
    //sample query taken from php manual, see link below
    do {
        /* store first result set */
        if ($result = $connection->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);
            }
            $result->free();
        }
        /* print divider */
        if ($connection->more_results()) {
            printf("-----------------\n");
        }
    } while ($connection->next_result());

}
else
{
    //handle error
    echo mysqli_error ( $connection );
}

If you check out the documentation of multi query (here), you will see that the function returns a boolean.

Source code also taken from php manual.

Matthias Bö
  • 449
  • 3
  • 12
  • unfortunately it still doesn't care about the condition but executes straight away. thanks though – Riwi Jun 29 '18 at 21:21
  • I updated the code to actually grab hold of the result. Maybe it works now as expected - that is if i didn't include some copy pasta error ;) – Matthias Bö Jun 29 '18 at 21:22
2

multi_query() returns a boolean that indicates whether the first query was successful, it doesn't return a mysqli_result object.

You have to call the store_result() method to get the first result set.

$resultset = $connection->store_result();

To get subsequent result sets you use more_results() and next_result().

See the examples in the documentation.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • i tried it like this: if($resultset = $connection->store_result()) { do something }, but i didn't work either. maybe i'm missing sthing here? – Riwi Jun 29 '18 at 21:24
  • I don't know why it's not working for you, this is exactly what it says to do in the documentation. I generally recommend avoiding multi-query, it usually makes things more complicated. – Barmar Jun 29 '18 at 21:27
  • thank you for your help Barmar, i accepted Matthias' answer. – Riwi Jun 29 '18 at 21:28
  • "I generally recommend avoiding multi-query,". you are right on this point - i must use it for this app. thanks again! – Riwi Jun 29 '18 at 21:29