Home > Database > Mysql Tutorial > body text

How to Retrieve Single Column Values in MySQLi as a One-Dimensional Array?

Mary-Kate Olsen
Release: 2024-11-02 17:29:29
Original
227 people have browsed it

How to Retrieve Single Column Values in MySQLi as a One-Dimensional Array?

Retrieving Single Column Values in MySQLi

You encountered an issue where a MySQLi query was returning a multidimensional array instead of a one-dimensional array of email addresses. To rectify this, utilize the fetch_assoc() method to retrieve a single column value.

The updated code, incorporating fetch_assoc(), is as follows:

<code class="php">$query = "SELECT DISTINCT `EmailAddress` FROM `Emails` WHERE `JobID` = 1";
$result = $conn->query($query);
if (!$result) {
    printf("Query failed: %s\n", $mysqli->error);
    exit;
}
$rows = array();
while ($row = $result->fetch_assoc()) {
    $rows[] = $row['EmailAddress'];
}</code>
Copy after login

By calling $result->fetch_assoc() within the loop, you instruct mysqli to fetch the next row from the result set and return it as an associative array, where the column names are used as array keys. The value of the desired column, in this case EmailAddress, can then be accessed using $row['EmailAddress'].

This update ensures that the $rows array contains a one-dimensional array of email addresses, as intended.

The above is the detailed content of How to Retrieve Single Column Values in MySQLi as a One-Dimensional Array?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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!