Question

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 />
Était-ce utile?

La solution 2

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;

Autres conseils

The error from your first coding attempt comes because you are storing the boolean result of mysqli_multi_query() as $test_result and then trying to feed $test_result to mysqli_fetch_assoc(). You intend to send the query result to the fetching function but that isn't how it's done with multi_query -- this is an understandably confusing versus how mysqli_query() works.

Here is a vital quote from the manual:

To retrieve the resultset from the first query you can use mysqli_use_result() or mysqli_store_result(). All subsequent query results can be processed using mysqli_more_results() and mysqli_next_result().

...This quote also touches on the issue with your second posted attempt which is only checking mysqli_next_result($link). Explicitly following the manual's instructions means writing the following while condition:

} while(mysqli_more_results($link) && mysqli_next_result($link));

The answer provided/selected by the OP is a workaround for not being able to find the solution. This whole $i business should be removed; you don't see this type of work at any of the mysqli docs pages.

Of the queries that you are running, only one of them should have a record set.

CREATE TEMPORARY TABLE //returns true/false. *structural, no rows in this case.

INSERT //returns true/false. *mysqli_affected_rows if desired.

UPDATE //returns true/false. *mysqli_affected_rows if desired.

SELECT //returns record set.  *max of 1 row in this case.

My proposed solution is:

if (mysqli_multi_query($link, $multi_test_query)) {
    do {
        if ($result = mysqli_store_result($link)) { // record set was generated for this query
            $satirsayisi = mysqli_fetch_row($result)[0];
            mysqli_free_result($result);
        }
    } while (mysqli_more_results($link) && mysqli_next_result($link));
}
if ($hata = mysqli_error($link)) {
    echo "Error / hata: $hata";  // don't display error messages when site is public
} else {
    echo "Number of lines / satır sayısı: $satirsayisi";
}

If there are any errors, $hata will be echoed.

If your SELECT query was successful, $satirsayisi will be your count value echoed to screen.

Additional advice/points:

Fetching From Single-Value-Result: using mysqli_fetch_row()[0] gets the job done in one line; no while loop needed.

Looping mysqli_multi_query(): Reference docs use DO WHILE loops so that the loop is always entered on the first iteration, and the condition statement is checked at the end of each iteration.

If there are any errors with any of the queries, mysqli_multi_query will cease executing subsequent queries -- this is how the function is designed to behave.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top