簡體   English   中英

將mysqli_query從函數添加到數組

[英]add mysqli_query to a array from a function

我試圖找出是否可以從另一個函數內的函數調用中更新數組。

我有一個包含大量HTML的函數,我認為將需要重復這些函數。 試圖找到不重復自己的解決方案。

我的初始功能是查詢創建數組的數據庫。 我打破了php並添加了HTML。

評論的部分是原始代碼,我試圖從中創建函數

function singleElim_displayTeam_r1() {
    global $conn;

    $sql = "SELECT * FROM teams";
    $result = mysqli_query($conn, $sql);
    $arrayTeams = array();

    // if(mysqli_num_rows($result) > 0) {
    //     while($row = mysqli_fetch_assoc($result)){
    //         $arrayTeams[] = $row;
    //     }
    // }

    createArrayRoundNumber("r1", $result, $arrayTeams);

    // print_r($arrayTeams);

    $num_rows = mysqli_num_rows($result);

    if(mysqli_num_rows($result) > 0) {
        for ($x = 0; $x < $num_rows; $x += 2) {
            $y = $x +1;
    ?>

函數調用

function createArrayRoundNumber($round, $result, $arrayTeams) {
    switch ($round) {
        case "r1":
            if(mysqli_num_rows($result) > 0) {
                while($row = mysqli_fetch_assoc($result)){
                     $arrayTeams[] = $row;
                }
            }
            break;

    }
}

這不會生成數組。 尋找幫助,將數組數據從原始singleElim_displayTeam_r1()推送到$ arrayTeams數組

您需要通過引用傳遞$ arrayTeams。 您需要在函數的$ arrayTeams前面添加“&”。

function createArrayRoundNumber($round, $result, &$arrayTeams) {
  switch ($round) {
    case "r1":
      if(mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)){
          $arrayTeams[] = $row;
        }
      }
     break;
   }
}

暫無
暫無

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

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