使用PHP在查询中运行查询时命令不同步

时间:2015-03-09 12:12:27

标签: php mysql nested sync

我正面临一个问题,我试图在查询中运行查询。我正在使用一个PHP脚本,它将进行api调用以更新跟踪详细信息到ebay销售记录。

它的工作方式是首先在表上选择任何未完成的记录。然后对于第一条记录,它将进行api调用,然后在响应中获取ack。如果Ack成功,它将更新销售完成,并插入日志记录。如果失败,则不会更新记录,并再次插入日志记录。然后它将转到下一个记录并重复相同的操作,等等其余的销售。

现在,当我去运行它时,我会收到"命令不同步的消息;你现在不能运行这个命令"。这只发生在第二张销售记录和之后的任何记录中。

我尝试关闭连接和free_result,但似乎没有任何效果。

我正在考虑将所有销售存储在一个数组中,但如果这很聪明或可能,我不会。

我注意到这个post有一个解决方案,但我似乎无法将它应用到我的场景中。请任何帮助将不胜感激!

这是我的代码:

<?php
echo "Start update of sales";

//Database query for pending orders
$servername = "localhost";
$username = "XXX";
$password = "XXXX";
$dbname = "XXXX";
$conn = mysqli_connect($servername, $username, $password, $dbname);
//Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$query = "SELECT ss.`item_id`, ss.`transaction_id`, ss.`tracking_number` FROM `sales_table` ss WHERE ss.`completed`<> 1";   
$result = mysqli_query($conn, $query) or die(mysql_error());
while($row = mysqli_fetch_array($result)){
echo $row['transaction_id']. " - ". $row['item_id'];
echo "<br />";
$tranid = $row['transaction_id'];
echo $tranid;

///Build the request Xml string
$requestXmlBody = '<?xml version="1.0" encoding="utf-8"?>';
$requestXmlBody .= '<CompleteSaleRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
...
$requestXmlBody .= '</CompleteSaleRequest>';    

//Create a new eBay session 
$session = new eBaySession($userToken, $devID, $appID, $certID, $serverUrl, $compatabilityLevel, $siteID, $verb);
//send the request and get response
$responseXml = $session->sendHttpRequest($requestXmlBody);
//print $responseXml;
PrintUtils::printXML($responseXml);
$xml = simplexml_load_string($responseXml);

foreach ($xml->Ack as $Ack) {  
        echo "Status: ".$Ack ."<br>";

        { $sql = "UPDATE `sales_table` SET `completed`=1, `completed_date`=now(), `update_date`=now() where `transaction_id`='$tranid' and `item_id`='$itemid';";
          $sql .= "INSERT INTO `sales_log` (`item_id`, `transaction_id`, `status`, `short_desc`, `long_desc`, `log_date`) VALUES ('$itemid', '$tranid', '$Ack', '$Ack', '$Ack', CURRENT_TIMESTAMP);";
    }}

    if (mysqli_multi_query($conn, $sql)){

    echo "New record created successfully <br>";
        } else {
            echo "Error inserting records. " . $sql ."<br>" . mysqli_error($conn);
        }} 
mysqli_free_result($result);mysqli_close($conn);?>

我仍在学习PHP,所以感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

这是怎么做到的?

$rows=array();
if($result=mysqli_query($conn,$query)){
    while(($rows[]=mysqli_fetch_assoc($result)) || array_pop($rows));
    mysqli_free_result($result);
}else{
    die(mysqli_error($conn));
}

foreach($rows as $row){
    $tranid=$row['transaction_id'];
    echo $row['transaction_id']." - ".$row['item_id']."<br>";

    //Build the request Xml string
    //Create a new eBay session 
    //send the request and get response
    //print $responseXml;

    $sql=array();
    foreach($xml->Ack as $Ack){
        echo "Status: ".$Ack."<br>";
        $sql["Update"]="UPDATE `sales_table` SET `completed`=1, `completed_date`=now(), `update_date`=now() where `transaction_id`='$tranid' and `item_id`='$itemid'";
        $sql["Insert"]="INSERT INTO `sales_log` (`item_id`, `transaction_id`, `status`, `short_desc`, `long_desc`, `log_date`) VALUES ('$itemid', '$tranid', '$Ack', '$Ack', '$Ack', CURRENT_TIMESTAMP)";
    }

    if(mysqli_multi_query($conn,implode(';',$sql))){
        do{
            list($current_key,$current_query)=each($sql);   //advances array pointer to first or next element
            if(mysqli_affected_rows($conn)<1){
                echo "Query logic error @ $current_key where TranID=$tranid & Ack=$Ack<br>";
            }else{
                echo "New record created where TranID=$tranid & Ack=$Ack<br>";
            }
        } while(mysqli_more_results($conn) && mysqli_next_result($conn));
    }else{
        list($current_key,$current_query)=each($sql);   //advances array pointer to first element
    }
    if($error_mess=mysqli_error($conn)){
        echo "Query syntax error @ $current_key where TranID=$tranid & Ack=$Ack<br>$error_mess<br>";
    }
    $error_mess=""; // clear for next iteration
} 
mysqli_close($conn);