我有一个PHP问题让我有点疯狂(可能是因为我不是专家 - 我确信有解决方案!)。
我需要做的是:
1)运行PHP脚本(readValues.php),从MySQL数据库中读取某些值
2)用这些值做一些事情来产生结果
3)根据结果,触发另一个PHP脚本(writeValues.php),该脚本每秒执行一次对MySQL数据库的写入。这个脚本可能运行很多分钟,所以我希望它在后台运行
4)一旦知道结果,就从readValues.php向用户发送响应。这就是我的问题所在 - readValues.php在向Web客户端发送响应之前等待writeValues.php完成
我尝试了很多来自SO的建议,包括:
然而,无论我尝试什么,readValues.php 总是在向用户发送响应之前等待writeValues.php完成。
以下是readValues.php的内容:
<?php
include 'aorigin.php';
include 'conn.php';
$data = array("userID" => (int)htmlspecialchars($_POST["userID"]),
"userMaxTroops" => null,
"userCurrentTroops" => null,
"troopLosses" => 0,
);
function randBetween ($min, $max) {
// generates a random number between (and including) the minimum and maximum parameters
$num = 0;
$num = (mt_rand() / mt_getrandmax()) * ($max - $min) + $min;
return $num;
}
// get the user's current troops
$sql = "SELECT current_troops FROM Users WHERE user_ID ='" . $data["userID"] . "';";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$data["userCurrentTroops"] = (int)$row["current_troops"];
$result -> close();
// determine troops lost
$data["troopLosses"] = round (randBetween (0, 5));
// send back the full data array - should only be used for testing and not in production!
echo json_encode ($data);
mysqli_close($conn);
// fire increment database script
if ($data["userCurrentTroops"] < $data["userMaxTroops"]) {
exec("nohup php incrementTroops.php " . $data['userID'] . " " . 5);
// return(exec("php incrementTroops.php " . $data['userID'] . " " . 5));
// exec("php incrementTroops.php " . $data['userID'] . " " . 5, $output);
// file_put_contents('file.txt', $output); // testing output of incrementTroops.php
// exit();
}
// include 'incrementTroops.php';
?>
以下是writeValues.php的内容:
<?php
include 'aorigin.php';
include 'conn.php';
$increment = FALSE;
$troopsPerSquare = $argv[2];
$data = array("userID" => (int)$argv[1],
"userSquares" => null,
"userMaxTroops" => null,
"userCurrentTroops" => null,
);
do { //loop will run until we do not need to increment
$increment = TRUE;
// get the user's squares count
$sql = "SELECT COUNT(User_ID) AS numberOfSquares FROM Map WHERE User_ID = '" . $data["userID"] . "';";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$data["userSquares"] = (int)$row["numberOfSquares"];
$result -> close();
// get the user's current troops
$sql = "SELECT current_troops FROM Users WHERE user_ID ='" . $data["userID"] . "';";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$data["userCurrentTroops"] = (int)$row["current_troops"];
$result -> close();
// set the user's max troops
$data["userMaxTroops"] = $data["userSquares"] * $troopsPerSquare;
sleep(1); //wait for 1 sec before executing
$sql = "UPDATE Users SET current_troops = current_troops + 1 WHERE user_ID ='" . $data["userID"] . "';";
$result = mysqli_query($conn, $sql);
$data["userCurrentTroops"] += 1;
// set $increment as FALSE if we want get out of this loop
if($data["userCurrentTroops"] >= $data["userMaxTroops"]){
$increment = FALSE;
}
} while ($increment == TRUE);
mysqli_close($conn);
?>
请注意以上是非常原始的测试代码,所以我还没有编写函数或添加错误检查等,直到我知道我可以解决这个基本问题。
我真的很感激任何帮助!