0

我正在尝试重写一段代码,该代码使用多个 mysql_query 调用来使用一个 mysqli_... 但无法使其正常工作。如果我只是在同一个地方用 mysqli_query 语句替换我的 mysql_query 语句,我会得到“命令不同步;你现在不能运行这个命令”

如果我尝试将所有查询拉入 mysqli_multi_query 中,则会收到“注意:未定义变量:第 14 行 /home/almostes/public_html/addrUpdate-proc-multi.php 中的 activeUser”错误。

简而言之,这就是我所拥有的

// 获取所有活跃用户的列表

SELECT entity_id FROM customer_entity_int WHERE attribute_id = 153 and value = 1 ORDER BY entity_id

// 循环遍历这个 entity_ids 列表,检查是否有针对该用户存储的地址

SELECT * FROM customer_address_entity WHERE parent_id = $entity_id;

// 如果没有针对该用户存储的地址,则获取存储在 customer_entity_varchar 中的信息

SELECT * FROM customer_entity_varchar WHERE entity_id = $entity_id;

// 然后将其输入到适当的地址表中

INSERT INTO customer_address_entity VALUES (blah, blah, blah);

我想我需要用类似的东西替换它

$query = "SELECT entity_id FROM customer_entity_int WHERE attribute_id = 153 and value = 1 ORDER BY entity_id;";    # get list of all active users#
$query .= "SELECT * FROM customer_address_entity WHERE parent_id = $entity_id;";    # is there an address stored against this name
$query .= "SELECT * FROM customer_entity_varchar WHERE entity_id = $entity_id;";    # get the info stored in customer_entity_varchar
$query .= "INSERT INTO customer_address_entity VALUES (blah, blah, blah);"

$result = mysqli_multi_query($mysqli, $query);

/* execute multi query */
if ($result) {
    do {
        /* store first result set */
        if ($result = mysqli_store_result($mysqli)) {
            while ($row = mysqli_fetch_assoc($result)) {
                $entity_id = $row['entity_id'];
        echo "<br />active user = $entity_id<br />";

        }
        mysqli_free_result($result);
    }
    /* print divider */
    if (mysqli_more_results($mysqli)) {
        printf("-----------------\n");
    }
      if (($result = mysqli_num_rows($mysqli)) < 1) // if no address stored against this user retrieve the registration data and enter into address data fields
      {
            echo '<br />yes<br />';
      }
    } while(mysqli_more_results($mysqli) && mysqli_next_result($mysqli));
}

但是a)这给出了一个“注意:未定义的变量:第14行的/home/almostes/public_html/addrUpdate-proc-multi.php中的activeUser”错误,其中第14行是“$query .=”SELECT * FROM customer_address_entity WHERE parent_id = $entity_id;";"

然后给出回显的输出

"活跃用户 = 5

活跃用户 = 6

活跃用户 = 78"

b)我看不到通过不同 sql 查询移动以获取没有地址信息的用户并检索所需字段的语法

任何帮助将非常感激。非常感谢

4

1 回答 1

0

显然,你不能以这种方式使用 mysqli-multi-query。而且,老实说,你也不需要它。

因此,只需按照通常的方式逐一运行您的查询。并念出圣奥卡姆的祈祷词:“entia non sunt multiplicanda praeter necessitatem”,意思是“实体的数量不得超过必要的数量”。

下次,当您在代码中遇到错误时,请就该错误提出问题,而不是建立更复杂且容易出错的结构。

于 2014-04-02T04:50:41.030 回答