使用mysqli_multi_query提取一些数据并在另一个表中重新输入

时间:2014-04-01 23:37:39

标签: php mysqli mysqli-multi-query

我正在尝试重写一段代码,该代码使用了几个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)这给了一个 &#34;注意:未定义的变量:第14行&#34; /home/almostes/public_html/addrUpdate-proc-multi.php中的activeUser;错误,第14行是&#34; $ query。=&#34; SELECT * FROM customer_address_entity WHERE parent_id = $ entity_id;&#34;;&#34;

然后给出回显输出

&#34;活跃用户= 5

活跃用户= 6

活跃用户= 78&#34;

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

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

1 个答案:

答案 0 :(得分:0)

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

因此,只需按常规方式逐个运行查询。并说圣奥卡姆的祷告:&#34; entia non sunt multiplicanda praeter necessitatem&#34;对于&#34;实体不得超过必要性&#34;。

下次,当您在代码中遇到错误时,请询问有关此错误的问题,而不是设置更复杂且容易出错的构造。