0

In MySQL I can perform multiple commands at once, for example:

Select * from Users; Select * from Classes;

How can I do the same when invoking the command from PHP, for example:

$sql=mysqli_query($link, "select * from Users;");
while($rows=mysqli_fetch_array($sql)){
    echo "<div>$rows[0]</div><div>$rows[1]</div>";
}

However, it doesn't work:

$sql=mysqli_query($link, "select * from Users; Select * from Classes");
while($rows=mysqli_fetch_array($sql)){
    echo "<div>$rows[0]</div><div>$rows[1]</div>";
}

I understand that in practice this may not be necessary. Consider I need this particular issue to simulate or to develop my own version of an interface using PHP to manage MySQL database.

3
  • 2
    Use concatenated statements. See here: stackoverflow.com/questions/10610675/… Commented Apr 19, 2013 at 4:57
  • 1
    You have to fetch both separately. Commented Apr 19, 2013 at 4:58
  • What do you expect as result from executing two select statements like that? How will you retrieve the found data? Please rethink your question hard...there probably is something wrong with your approach in general. Commented Apr 19, 2013 at 7:46

3 Answers 3

3

you can achieve this through "multi_query" option.

$query = "select * from Users;";
$query .= "Select * from Classes";



if (mysqli_multi_query($link, $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));
}
mysqli_close($link);
Sign up to request clarification or add additional context in comments.

6 Comments

There really is no point executing 2 SELECT statements together with mysqli_multi_query, since you will never be able to fetch the results of the first SELECT statement.
@Jocelyn - you can fetch the results of both the select statements i have tried and i have got the result as well....
@sandy And how do you address the different queries? How do you keep the two result sets apart?
its the "mysqli_more_results " which identifies different queries. Here i have separated with "-----------" which does the job. And "mysqli_next_result" breaks the connection of multiple queries and then after this statement you can use the regular queries as before...
Okay. Interesting. Never seen that before. So the question remaining is probably whether there is any advantage to doing this instead of two separate queries, which will make the code a lot simpler. Especially when you need to access fields of the results and they are different for each of the queries.
|
0

In MySQL [console] I can perform multiple commands once

yes, you can.

But technically they are executed separately anyway.

So, 2 consequent calls to mysqli_query() will do exactly the same.

Thus, there is no problem to make 2 calls instead of one. Especially because there is not a single point in doing all at once.

Although you can use whatever multi-query solution, it will make not much sense and just make your code overcomplicated and error-prone for no reason.

Comments

-1

You can not run two query simultaneously,by defaul mysql always take last query after semi column i. it will run Select * from Classes.Instead of doing this better to take union of both query.

1 Comment

pls check about mysqli_multi_query in the php manual

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.