用mysqli_query替换mysql_query并获取'命令不同步'错误

时间:2014-04-02 07:14:51

标签: php mysqli

您好我正在尝试用mysqli_query重写一段替换mysql_query的代码,但我收到一条command out of sync消息。

我认为这意味着我需要使用mysqli_multi_query,但显然不是,这应该有效 -

using mysqli_multi_query to extract some data and re-enter it in a different table

我尝试添加MYSQL_STORE_RESULT并释放$result资源,但无法通过此不同步错误。我需要做些什么才能使这些数据库调用快速且随后运行。

$result = mysqli_query($con,"SELECT entity_id FROM customer_entity_int WHERE attribute_id = 153 and value = 1 ORDER BY entity_id;") or die(mysqli_error($con)); // this selects the entity id for all active registered users
// $result = mysqli_query($con,"SELECT entity_id FROM customer_entity_int WHERE attribute_id = 153 AND value = 1 AND entity_id > 453 AND entity_id < 455 ORDER BY entity_id;");

while($row = mysqli_fetch_array($result))
{

$activeUser = $row['entity_id'];
//
    echo "<br />-<br />active user = $activeUser<br />";

$res_CheckAddRef = mysqli_query($con,"SELECT * FROM customer_address_entity WHERE parent_id = $activeUser;") or die(mysqli_error($con));    // check to see if there is an address stored against this user
//
    echo "SELECT * FROM customer_address_entity WHERE parent_id = ".$activeUser."<br />";


if(mysqli_num_rows($res_CheckAddRef) < 1)       // if no address stored against this active user retrieve the registration data and enter into address data fields
{
//
        echo "<br />my sql num rows = ".mysqli_num_rows($res_CheckAddRef)."<br />";
    $res_GetAddrData = mysqli_query($con,"SELECT * FROM customer_entity_varchar WHERE entity_id = $activeUser;") or die(mysqli_error($con));
//
        echo "SELECT * FROM customer_entity_varchar WHERE entity_id = $activeUser;<br />";


    while($row = mysqli_fetch_assoc($res_GetAddrData))  {
        switch($row['attribute_id']) {
            case 5:
                $fname = $row['value'];
                break;
            case 7:
                $lname = $row['value'];
                break;
            case 156:
                $street = strtoupper($row['value']);
                break;
            case 151:
                $pcode = strtoupper($row['value']);
                break;
            case 157:
                $telephone = $row['value'];
                break;
        }
    }
        echo "<br />$fname, $lname, $street, $pcode, $telephone<br />";

    ## check we have retrieved address data
    if($fname != '' && $lname != '' && $street != '' && $pcode != '' && $telephone != '') {
        ## START CREATING THE NEW RECORD ##
        ## insert entity_id (auto increment), entity_type_id (2), attribute_set_id (0), increment_id(NULL), parent_id (active user), created at & modified at (now date()), is_active (1)
        date_default_timezone_set('GMT');
        $res_CreateNewAddr = mysqli_query($con,"INSERT INTO customer_address_entity VALUES ('','2','0',NULL,'$activeUser','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."','1');");
            echo "INSERT INTO customer_address_entity VALUES ('','2','0',NULL,'$activeUser','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."','1');"; echo "<br />";
        if(mysqli_affected_rows($con) < 1) {
            echo "res_CreateNewAddr didn't work. ".mysqli_error()."<br />";
        }

这将运行一次,但然后给出

Commands out of sync; you can't run this command now

第二次迭代时出现

错误。

非常感谢你能放下任何光明。

0 个答案:

没有答案