?
Solved

multi_query usage

Posted on 2009-07-09
6
Medium Priority
?
890 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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

771 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