Home >Database >Mysql Tutorial >How to Construct PHP Arrays from MySQL Column Data?

How to Construct PHP Arrays from MySQL Column Data?

Susan Sarandon
Susan SarandonOriginal
2024-10-23 20:17:02920browse

How to Construct PHP Arrays from MySQL Column Data?

Constructing PHP Arrays from MySQL Column Data

Retrieving data from a MySQL column using mysql_fetch_array results in an array representing a single row. To create an array consisting of values from all rows in a specific column, an effective approach involves iterating through the array and assembling a new array:

<code class="php">$column = array();

while ($row = mysql_fetch_array($info)) {
    $column[] = $row[$key]; // Append the value of the column being accessed to the new array
}</code>

By incrementally adding values from each row to the $column array, you can create an array that encompasses the entire column's data. Remember to specify the appropriate $key to indicate the column from which you want to extract values.

The above is the detailed content of How to Construct PHP Arrays from MySQL Column Data?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn