[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

multi_query usage

Posted on 2009-07-09
6
Medium Priority
?
900 Views
Last Modified: 2013-12-13
Hi.

I want to use mysqli multi_query function.
However I am facing with the following problem.

Can you explain why it gives an error and what is the solution for that.
$conn = new mysqli(host, user, pass, dbname) or die(mysqli_connect_error());
 
$d = $conn->query("SELECT COUNT(id) FROM table2");
while ($row = $d->fetch_row())
{
    echo $row[0];
}
exit();
 
 
 
The following throws an error:
 
$conn->multi_query("INSERT INTO table2(name) VALUES('a'); INSERT INTO table2(name) VALUES('b');");
$d = $conn->query("SELECT COUNT(id) FROM table2");
while ($row = $d->fetch_row())
{
    echo $row[0];
}
exit();
 
error:
 
Fatal error: Call to a member function fetch_row() on a non-object in .....

Open in new window

0
Comment
Question by:jet-black
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24812613
First, you don't need a to send multiple queries when inserting multiple rows to the same table. You could insert both rows with one insert statement, like this:

INSERT INTO table2(name) VALUES('a'),('b');

You should allways test for errors:

$d = $conn->query("SELECT COUNT(id) FROM table2");
if(!$d) die($conn->error);

This would show you this error:

Commands out of sync; you can't run this command now

When using multi_query(), you need to handle the results before you try to send more queries on the same connection. In this case there is no rows to fetch, so you can just skip each result, like this:

$conn->multi_query("INSERT INTO table2(name) VALUES('a'); INSERT INTO table2(name) VALUES('b');");
while($conn->more_results()) $conn->next_result();

http://php.net/manual/en/mysqli.multi-query.php
http://php.net/manual/en/mysqli.more-results.php
http://php.net/manual/en/mysqli.next-result.php
0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24812625
try this...

while($row = fetch_row($d))

instead of this...

while ($row = $d->fetch_row())



Alan
0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24812635
opps, sorry crx. I didnt see your post before I posted.

Alan
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:xBellox
ID: 24812650
It appears that  this error isn't from "multi_query()" function, it occurs because the line:

$d = $conn->query("SELECT COUNT(id) FROM table2");

Do not create an object in $d variable, so when you call "$d->fetch_row()" it give you the error that fetch_row() must be called from an object which $d is not.

Try something like this:


if ($d = $conn->query("SELECT COUNT(id) FROM table2")) {
    while ($row = $d->fetch_row()) {
        echo $row[0];
    }
    $d->close();
    exit();
}

Open in new window

0
 
LVL 12

Author Closing Comment

by:jet-black
ID: 31601518
Thank you for the true solution.
INSERT statements were just for demonstration :)
0
 
LVL 12

Author Comment

by:jet-black
ID: 24812874
xBellox, thank you for your message.
However,

if ($d = $conn->query("SELECT COUNT(id) FROM table2")) {
    while ($row = $d->fetch_row()) {
        echo $row[0];
    }
    $d->close();
    exit();
}

The code block within if statement doesn't run because $d = $conn->query("SELECT COUNT(id) FROM table2") gives an error which is not return TRUE.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question