We select all identifiers and put them in an array in PHP

How do I get an SQL script to collect all identifiers from a field and put them in an array?

+3
source share
2 answers

Assuming the identifier column is just named id, try the following code (DB cell code for clarification):

$ ids_array = array ();

$ result = mysql_query ("SELECT id FROM table_name");

while ($ row = mysql_fetch_array ($ result))
{
    $ ids_array [] = $ row ['id'];
}

If you want to sort identifiers in ascending or descending order, simply add ORDER BY id ASCor ORDER BY id DESCrespectively to the end of the MySQL query

, , . $row - , id, (SELECT id FROM ...). $ids_array[] , ($ids_array, $row['id'] ( ), .

while print_r($ids_array);, , .

+6

ID sqlQuery , - :

$query=mysql_query("SELECT * FROM table WHERE...");
while($row=mysql_fetch_assoc($query)){
   $array[]=$row['id'];
}
0

All Articles