Home >Database >Mysql Tutorial >How to Generate an Array from a Specific MySQL Column in PHP?
Populating PHP Arrays with MySQL Column Data
MySQL's mysql_fetch_array function provides an array representation of a single database row. However, there are scenarios where you may wish to construct an array incorporating values from a specific column across all rows. Here's an effective approach to accomplish this:
Solution:
To generate an array containing all values from a particular column, you can utilize a while loop in conjunction with the mysql_fetch_array function:
<code class="php">$column = array(); while ($row = mysql_fetch_array($info)) { $column[] = $row[$key]; // Store column value in the array } // Note: Semicolons are added at the end of lines for proper syntax.</code>
In this code snippet:
This approach allows you to conveniently work with specific column data in a PHP array, enabling efficient processing and data manipulation tasks.
The above is the detailed content of How to Generate an Array from a Specific MySQL Column in PHP?. For more information, please follow other related articles on the PHP Chinese website!