簡體   English   中英

使用mysqli_multi_query提取一些數據並將其重新輸入到另一個表中

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

我正在嘗試重寫一段代碼,該代碼使用多個mysql_query調用來使用一個mysqli _...,但無法使其正常工作。 如果我只是在同一位置用mysqli_query語句替換mysql_query語句,則會收到“命令不同步;您現在不能運行此命令”

如果我改為嘗試將所有查詢拉入mysqli_multi_query,則會收到“通知:未定義的變量:/home/almostes/public_html/addrUpdate-proc-multi.php在第14行上的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)這會導致“通知:未定義的變量:/home/almostes/public_html/addrUpdate-proc-multi.php在第14行上的錯誤”錯誤,其中第14行是“ $ query。=” SELECT * FROM customer_address_entity WHERE parent_id = $ entity_id;“;”

然后給出回顯的輸出

“活動用戶= 5

活動用戶= 6

活動用戶= 78英寸

和b)我看不到在不同的sql查詢中移動以獲取沒有地址信息的用戶並檢索所需字段的語法

任何幫助將非常感激。 非常感謝

顯然,您不能以這種方式使用mysqli-multi-query。 而且,老實說,您也不需要它。

因此,只需一種通常的方式就可以一次運行查詢。 並說一下聖奧卡姆的祈禱:“不可增加實體”。

下次,當您在代碼中遇到錯誤時,請問一個有關此錯誤的問題,而不是建立更復雜且易於出錯的構造。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM