Creating an Array from a Database Column in PHP
Extracting a single row's data from a database using mysql_fetch_array results in an array. However, to create an array from the values of all rows in a specific column, a more comprehensive approach is required.
Solution
One effective solution is to loop through the rows fetched from the database and progressively build an array of column values:
<code class="php">$column = array(); // Initialize an empty array while ($row = mysql_fetch_array($info)) { $column[] = $row[$key]; // Append the value of the desired column to the array }</code>
In this code:
This process continues until all rows have been processed, resulting in an array containing the values of the desired column from all rows.
The above is the detailed content of How to Create an Array from a Database Column in PHP?. For more information, please follow other related articles on the PHP Chinese website!