PHP页面加载中查询(MySQL)

时间:2017-03-19 12:09:36

标签: php mysql mysqli multi-query

我有一个带有HTML表单的PHP页面,用于更改MySQL数据库中的某些信息。表单提交到另一个PHP页面,该页面根据从表单收到的信息进行大约7-10个查询。这些查询按特定顺序排列非常重要。我正在使用mysqli::multi_query来执行这些查询。在我执行查询后的最后,我使用header("Location: " . $_SERVER['HTTP_REFERER']);将用户返回到包含表单的页面。当用户返回到上一页时,我的问题就出现了。页面在查询中间出现的时间点加载,而不是在完成的产品中加载。如果我然后点击刷新它加载更新的信息。如何阻止我的页面加载,直到它可以从数据库中获取更新的信息而不是加载中间查询?

编辑以添加代码(即使我认为它与基础问题无关):

$sql = "SELECT * FROM tool_categories";
if($result = $MySQLi->query($sql)){
    $toolCategories = array();
    while($row = $result->fetch_assoc()){
        $toolCategories[] = $row;
    }
    $result->free();
}

if(isset($_POST['editCategory'])){ // Editing category
    if(!in_array($_POST['categoryName'], $toolCategories)){ // Make sure it doesn't exist already
        $sql = "UPDATE tool_categories SET categoryName='" . $_POST['categoryName'] . "' WHERE categoryID=" . $_POST['categoryID'];
        if($_POST['placement'] != 0){
            if(!in_array($_POST['placement'], array_column($toolCategories, 'categoryID'))){ // Check if it exists
                $sql .= "; UPDATE tool_categories SET categoryID=" . $_POST['placement'];
            }else{
                // Welp, gotta make some changes to categoryID's to make this fit!
                $sql = "UPDATE tool_categories SET categoryID=0 WHERE categoryID=" . intval($_POST['categoryID']) . ";";
                $sql .= "UPDATE tool_categories SET categoryID=categoryID-1 WHERE categoryID >= " . intval($_POST['categoryID']) . ";";
                $sql .= "UPDATE tools SET categoryID=categoryID-1 WHERE categoryID >= " . intval($_POST['categoryID']) . ";";
                $sql .= "ALTER TABLE tool_categories DROP INDEX categoryID;";
                $sql .= "ALTER TABLE tool_categories DROP PRIMARY KEY;";
                $sql .= "UPDATE tool_categories SET categoryID=categoryID+1 WHERE categoryID >= " . intval($_POST['placement']) . ";";
                $sql .= "UPDATE tools SET categoryID=categoryID+1 WHERE categoryID >= " . intval($_POST['placement']) . ";";
                $sql .= "ALTER TABLE tool_categories ADD INDEX categoryID (categoryID);";
                $sql .= "ALTER TABLE tool_categories ADD PRIMARY KEY(categoryID);";
                $sql .= "UPDATE tool_categories SET categoryID=" . intval($_POST['placement']) . ", categoryName='" . $_POST['categoryName'] . "' WHERE categoryID=0";
            }
        }
    }
}


$startQuery = microtime(true);
$numberOfQueries = count(explode(';', $sql));
if(!$MySQLi->multi_query($sql)){
    die(db_error());
    for($i = 2; $i < $numberOfQueries+1; $i++){
        if(!$MySQLi->next_result()){
            die(db_error());
        }
    }
}
$endQuery = microtime(true);
$queryTime = $endQuery - $startQuery;
header("Location: " . $_SERVER['HTTP_REFERER'] . "&queryTime=" . $queryTime . "&queries=" . $numberOfQueries);

2 个答案:

答案 0 :(得分:1)

以下代码参考@mickmackusa在上述评论中提供的以下帖子。 Strict Standards: mysqli_next_result() error with mysqli_multi_query

if($MySQLi->multi_query($sql)){
    do{} while($MySQLi->more_results() && $MySQLi->next_result());
}
if($error_mess = $MySQLi->error){ die("Error: " . $error_mess); }

此代码设法阻止我的下一页加载,直到所有查询按预期完成。

答案 1 :(得分:0)

你应该避免 public Actor(Image img, int x,int y, int width, int height){ this.x=x; this.y=y; this.width=width; this.height=height; this.img = img; } 。而是逐个单独运行查询。它会阻止您的页面加载,直到它可以获取更新的信息。