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

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);
$array = array(1,2,3,4,5);

Use below SQL query for selecting the array id data

SELECT column_name(s)
FROM table_name
WHERE column_name IN $array;

Collect necessary values into an array.

$a = [];
while(...){          
    $a[] = $row['id_add_user'];
}
echo implode(',', $a);

You are trying to implode() the values for each row, you need to build an array of all the values and then output the result imploded. Also if you just want one column - just fetch that column in your SQL

You can further simplify it to...

$query = mysqli_query($conn, "SELECT id_add_user FROM tableA");
$rows = mysqli_fetch_all($query );
echo implode(',',array_column($rows, 'id_add_user' ));

mysqli_fetch_all allows you to fetch all the data in one go. Then use array_column() to extract the data.