Trying to get value from PHP multi_query

2019-08-13 23:11发布

问题:

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....

回答1:

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.



回答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.



标签: php mysqli