Home > Database > Mysql Tutorial > body text

How to Create an Array from a Database Column in PHP?

Linda Hamilton
Release: 2024-10-24 05:22:30
Original
936 people have browsed it

How to Create an Array from a Database Column in PHP?

Creating an Array from a Database Column in PHP

Extracting a single row's data from a database using mysql_fetch_array results in an array. However, to create an array from the values of all rows in a specific column, a more comprehensive approach is required.

Solution

One effective solution is to loop through the rows fetched from the database and progressively build an array of column values:

<code class="php">$column = array(); // Initialize an empty array

while ($row = mysql_fetch_array($info)) {
    $column[] = $row[$key]; // Append the value of the desired column to the array
}</code>
Copy after login

In this code:

  • An empty array $column is initialized.
  • A while loop iterates through the rows retrieved from the database using mysql_fetch_array.
  • For each row, the value of the specified column is obtained using $row[$key], where $key is the column name or index.
  • The retrieved value is appended to the $column array.

This process continues until all rows have been processed, resulting in an array containing the values of the desired column from all rows.

The above is the detailed content of How to Create an Array from a Database Column in PHP?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!