Reputation: 101
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
Upvotes: 2
Views: 3287
Reputation: 57121
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 implode
d. 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.
Upvotes: 1
Reputation: 14982
Collect necessary values into an array.
$a = [];
while(...){
$a[] = $row['id_add_user'];
}
echo implode(',', $a);
Upvotes: 1
Reputation: 26450
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);
Upvotes: 2
Reputation: 286
$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;
Upvotes: -1