Search code examples
phpmysqliresultsetmysqli-multi-query

How to get the resultset from a SELECT query using mysqli_multi_query()?


My queries are:

CREATE TEMPORARY TABLE `hcaconsumptions_temp` (
    `DeviceID` INT (11) NOT NULL,
    `TimeStamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `Consumption` FLOAT NOT NULL,
    `DeviceBrand` VARCHAR (255) CHARACTER
SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 `SerialNumber` VARCHAR (255) CHARACTER
SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE = MyISAM DEFAULT CHARACTER
SET = utf8 COLLATE = utf8_unicode_ci;

INSERT INTO hcaconsumptions_temp 
    (DeviceBrand, SerialNumber, `TIMESTAMP`, Consumption) 
    VALUES
    ('Adunos','24100008','2013-01-14 19:39:48','157'),
    ('Adunos','24100010','2013-01-14 18:50:38','134'),
    ...
    ('Adunos','24100019','2013-01-14 18:40:58','117'),
    ('Adunos','24100020','2013-01-14 18:42:22','74');

UPDATE hcaconsumptions_temp
SET DeviceID = (
    SELECT
        DeviceID
    FROM
        hcadevices
    WHERE
        hcadevices.DeviceBrand = hcaconsumptions_temp.DeviceBrand
    AND hcadevices.SerialNumber = hcaconsumptions_temp.SerialNumber
);

SELECT
    count(DeviceID)
FROM
    hcaconsumptions_temp
WHERE
    DeviceID = '0';

As you can see at the end there is SELECT query, but I cannot get the results.

Probably because there are 3 queries prior, so it gives an error. How do I get the result?

My PHP code is:

$test_result = mysqli_multi_query($link, $multi_test_query);
$count = mysqli_fetch_assoc($test_result);
print_r($count);

The warning that is given is:

mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given

This code gives 2 errors:

$test_result = mysqli_multi_query($link, $multi_test_query);
$count = mysqli_fetch_assoc($test_result);
echo mysqli_error();
print_r($count);

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\Program Files (x86)\EasyPHP-12.1\www\App\php\post\excel_yukle.php on line 123

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\Program Files (x86)\EasyPHP-12.1\www\App\php\post\excel_yukle.php on line 124

Edit: I have tried this:

/* execute multi query */
if (mysqli_multi_query($link, $multi_test_query)) {
    do {
        /* store first result set */
        if ($result = mysqli_store_result($link)) {
            while ($row = mysqli_fetch_row($result)) {
                printf("%s\n", $row[0]);
            }
            mysqli_free_result($result);
        }
        /* print divider */
        if (mysqli_more_results($link)) {
            printf("-----------------\n");
        }
    } while (mysqli_next_result($link));
}

And the result was:

-----------------
-----------------
-----------------
46
<br />
<b>Strict Standards</b>:  mysqli_next_result(): There is no next result set. Please, call mysqli_more_results()/mysqli::more_results() to check whether to call this function/method in <b>C:\Program Files (x86)\EasyPHP-12.1\www\HCAWebApp\php\post\excel_yukle.php</b> on line <b>136</b><br />

Solution

  • I have solved it.

    $i = 1;
    if (mysqli_multi_query($link, $multi_test_query)) {
        while(mysqli_next_result($link)) {
            /* store first result set */
            if ($result = mysqli_store_result($link)) {
                while ($row = mysqli_fetch_row($result)) {
                    //printf("%s\n", $row[0]);
                    if($i==3) $satirsayisi = $row[0];
                }
                mysqli_free_result($result);
            }
            /* print divider */
            if (mysqli_more_results($link)) {
                //printf("-----------------\n");
            }
    
            $i = $i + 1;
            if($i>3) break;
        }
    }
    
    
    echo $satirsayisi;