Often, when working with databases, it's essential to retrieve individual rows of data. Typically, you might use the mysql_fetch_array() function to accomplish this. However, what if you need to create an array consisting of the data from a specific column across all retrieved rows?
The solution lies in iterating through the rows returned by mysql_fetch_array() and adding the desired column's value to a new array. Here's a code snippet to illustrate the process:
<code class="php">$column = []; // Initialize an empty array // Fetch rows from the database while ($row = mysql_fetch_array($info)) { // Append the value of the specified column to the $column array $column[] = $row[$key]; }</code>
By using this approach, you can easily generate an array that contains the values from a specific column across all the rows retrieved from the database.
The above is the detailed content of How to Populate a PHP Array with Column Data from MySQL?. For more information, please follow other related articles on the PHP Chinese website!