Home > Database > Mysql Tutorial > body text

How to Generate an Array from a Specific MySQL Column in PHP?

Linda Hamilton
Release: 2024-10-24 02:20:29
Original
279 people have browsed it

How to Generate an Array from a Specific MySQL Column in PHP?

Populating PHP Arrays with MySQL Column Data

MySQL's mysql_fetch_array function provides an array representation of a single database row. However, there are scenarios where you may wish to construct an array incorporating values from a specific column across all rows. Here's an effective approach to accomplish this:

Solution:

To generate an array containing all values from a particular column, you can utilize a while loop in conjunction with the mysql_fetch_array function:

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

while ($row = mysql_fetch_array($info)) {
    $column[] = $row[$key]; // Store column value in the array
}

// Note: Semicolons are added at the end of lines for proper syntax.</code>
Copy after login

In this code snippet:

  • $column will hold the values from the specified column.
  • The while loop iterates through each row in the $info array.
  • Within the loop, the value of the key column in each row is retrieved using $row[$key] and appended to the $column array.
  • The result is an array containing all column values from the database.

This approach allows you to conveniently work with specific column data in a PHP array, enabling efficient processing and data manipulation tasks.

The above is the detailed content of How to Generate an Array from a Specific MySQL 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!