dongpiansui8755 2018-05-11 07:17
浏览 48
已采纳

PHP / MySQL - 如何从表中选择id并在数组中选择echo

How to make the array of id that we call from a table?

What I want is like this :

$array = array(1, 2, 3, 4, 5); // **1 - 5 select from a table**.

Thank you

Code :

$query = mysqli_query($conn, "SELECT * FROM tableA");
while($row = mysqli_fetch_assoc($query )){          
    $a = implode(',',(array)$row['id_add_user']);
    echo $a;
}

What I get from echo $a is 12345 not 1,2,3,4,5

  • 写回答

4条回答 默认 最新

  • douhuocuo9012 2018-05-11 07:36
    关注

    Add all the elements to an array, then implode() it into one string with your desired deliminator (here, its ", ") once all results are fetched.

    $result = [];
    $query = mysqli_query($conn, "SELECT * FROM tableA");
    while($row = mysqli_fetch_assoc($query)){          
        $result[] = $row['id_add_user']);
    }
    
    echo implode(", ", $result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?