Home  >  Article  >  Database  >  How to Populate a PHP Array with Column Data from MySQL?

How to Populate a PHP Array with Column Data from MySQL?

Barbara Streisand
Barbara StreisandOriginal
2024-10-23 22:15:01878browse

How to Populate a PHP Array with Column Data from MySQL?

Populate PHP Array with Column Data from MySQL

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!

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