簡體   English   中英

訪問數組-多個數組

[英]Accessing Arrays - multiple arrays

我有以下內容:

庫MySQLi

$query = "SELECT company, AVG(q1) AS high_1 FROM tresults;"
$query .= "SELECT company, AVG(q2) AS high_2 FROM tresults;"

Var轉儲

array(2) { ["company"]=> string(8) "7yh6t5rf" ["high_1"]=> string(10) "7.65498709" }
array(2) { ["company"]=> string(8) "de4r5tgh" ["high_2"]=> string(10) "9.12375681" }

if ($mysqli->multi_query($query)) {
  do {
    /* store first result set */
      if ($result = $mysqli->store_result()) {
        while ($row = $result->fetch_assoc()) {
          for ($p=1; $p<=20; $p++)
            {
              echo"
                <td class='a25'>"; 
                  echo number_format($row["high_".$p],2);
              echo"</td>"; 
            } 
          $result->free();
        }
      } while ($mysqli->next_result());
    }
  /* close connection */
$mysqli->close();

產量

<td class='a25'>7.65</td><td class='a25'></td>

基本上,我遇到的問題是,上面的代碼僅回high_1而不是high_2

我無法理解為什么會這樣。 歡迎任何建議和反饋。

如果已實現,請嘗試使用fetch array而不是fetch_assoc函數,並且可以通過選擇索引訪問結果行

    while ($row = $result->fetch_array()) {
      echo "<td class='a25'>", 
           number_format($row[1],2),
           "</td>"; 
      $result->free();
    }

我無法解決問題。

因此,我使用的解決方案是將所有內容簡單添加到可以在while loop之外使用的單個數組中的一種:

while ($row = $result->fetch_assoc()) {
    $array[] = $row;
}

暫無
暫無

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

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