让PHP等待每个MySql查询执行

时间:2014-04-06 08:38:46

标签: php mysql

我想从一个php页面运行多个mysql查询,但我需要它“等待”,直到每个查询完成后再继续。看来目前它会同时加载它们。

是否有解决方法或函数使页面等到查询完成后再转到下一页?

- 编辑 -

所以这是我的代码

//These top three seem to work fine but I guess it doesn't matter if the next one starts before the last one ends
mysqli_query($con,"DELETE\n".
"FROM\n".
"   temp_cats");
mysqli_query($con,"DELETE\n".
"FROM\n".
"   temp_concats");
mysqli_query($con,"DELETE\n".
"FROM\n".
"   temp_locations");

这似乎工作正常,但我不会注意到如果一个人在结束之前就开始了。

下一个查询工作正常。它需要来自另一个表的查询中提到的国家并将其添加到新表中。更新查询在另一列中“标记”它们。

mysqli_query($con,"INSERT IGNORE temp_locations (temp_locations.Location) SELECT DISTINCT\n".
"Region\n".
"FROM\n".
"LocationsForExcel\n".
"WHERE\n".
"Country IN (" . $_SESSION['SearchCountriesQuery'] . ")");
//mark the new locations type as country
mysqli_query($con,"UPDATE temp_locations\n".
"SET LocationType = 'Country'\n".
"WHERE\n".
"   LocationType IS NULL");

当我把它放在下面时会出现问题:

mysqli_query($con,"INSERT IGNORE temp_locations (temp_locations.Location) SELECT DISTINCT\n".
"Region\n".
"FROM\n".
"LocationsForExcel\n".
"WHERE\n".
"Country IN" . $_SESSION['SearchCountriesQuery'] . ")");
//mark the new locations type as region
mysqli_query($con,"UPDATE temp_locations\n".
"SET LocationType = 'Region'\n".
"WHERE\n".
"   LocationType IS NULL");

最终结果是两个INSERT查询都运行正常但“LocationType”列都显示为“Country”。

我能想到的唯一事情就是查询没有按顺序运行或相互重叠。

有谁看到这里发生了什么?

1 个答案:

答案 0 :(得分:0)

这不是一个非常好的方法,但你可以做到这一点(显示一些代码会有很多帮助):

<?php
$query = //your query

if($query) {
 $query2 = //your 2nd query
}

if($query2) {
 $query3 = //your 3rd query
}
//and so on
?>
相关问题