我想在处理过程中等待php函数(来自另一个调用)

时间:2019-08-22 21:16:17

标签: php android mysql database prepared-statement

[语言:php]

我有一个具有三个mysql prepare语句的函数。 当函数已经在处理时从Api调用函数时..我想等待该调用并在处理完成后恢复...

实际上我的问题是...在我的职能上 我的第一个mysql prepare语句从数据库中选择一个值并绑定该结果。 第二个prepare语句使用该结果。

但是另一个调用从数据库中选择值,而无需等待第二个prepare语句从第一次调用中执行...

//function sample...

public function saveSales($query1,$query2,$query3){

//query1 execute and return a result from database
//using that result execute query 2
//execute query 3

// i want to run this three querys without interrupt from another call
}

我的实际代码:

[index.php:slimframework(Api)]

$app->post('/savesalesnew', function(Request $request, Response $response){

    $request_data = $request->getParsedBody(); 
    $sm= $request_data['sm'];
    $st= $request_data['st'];
    $trans = $request_data['trans'];
    $user= $request_data['user'];
    $pass = $request_data['pass'];
    $maxfind = $request_data['maxfind'];

    $db = new DbOperations; 
    $queryPostResult = $db->saveSales($sm,$st,$trans,$user,$pass,$maxfind);
    $response_data=$queryPostResult; 
    $response->write(json_encode($response_data));
    return $response
        ->withHeader('Content-type', 'application/json')
        ->withStatus(200);    


});

[数据库操作.php]

public function saveSales($sm,$st,$trans,$user,$pass,$maxfind){    


    $stmt = $this->con->prepare("SELECT emp_pass FROM emp WHERE emp_name ='$user'");
    $stmt->execute(); 
    $stmt->bind_result($password); 
                    $stmt->fetch();           

    if(strcmp($pass, $password) !== 0)  {
        return false;
    }     

    $stmt->close();



 $mysqli = new mysqli(//connection parameters);

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$mysqli->autocommit(FALSE);


 $stmt1 = $this->con->prepare("$maxfind");
    $stmt1->execute();
    $stmt1->bind_result($maxInvoice); 
                    $stmt1->fetch();
    $stmt1->close();


    $sm = str_replace("#%#@#@#23@#@3", $maxInvoice, $sm);
    $st = str_replace("#%#@#@#23@#@3", $maxInvoice, $st);
    $trans = str_replace("#%#@#@#23@#@3", $maxInvoice, $trans);
$mysqli->query("$sm");
$mysqli->query("$st");
$mysqli->query("$trans");

$mysqli->commit();

$mysqli->close();


return true;


}
//-----------------------------

1 个答案:

答案 0 :(得分:0)

如果服务器上的处理时间较长,则无论数据是否就绪,都需要在数据库中使用列标志。如果数据尚未准备就绪,则将“ 1”设置为“ 2”。