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

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

Barbara Streisand
Release: 2024-10-23 22:15:01
Original
955 people have browsed it

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>
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template