This question already has an answer here:
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.
</div>
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
", $row[0]);
}
mysqli_free_result($result);
}
/* print divider */
if (mysqli_more_results($link)) {
printf("-----------------
");
}
} while (mysqli_next_result($link));
}
mysqli_close($link);
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.
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.