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>
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!