php - 如何使用 MySQLi 获取单列值?

标签 php mysql arrays mysqli

我正在尝试从 MySQL 数据库中获取一维数组中的列表或电子邮件。 但是,我得到的是多维数组,而不是数组:

$query = "SELECT DISTINCT `EmailAddress` FROM `Emails` WHERE `JobID` = 1";
$result = $conn->query($query);
if (!$result) {
    printf("Query failed: %s\n", $mysqli->error);
    exit;
}
while ($row = $result->fetch_row()) {
    $rows[] = $row;
}
$result->close();
$conn->close();
var_dump($rows); // This will return array(2) { [0]=> array(1) { [0]=> string(24) "abc@abc.com" } [1]=> array(1) { [0]=> string(17) "hello@gmail.com" } }
//This is how array should be
$MyV = array("abc@abc.com", "hello@gmail.com");
var_dump($MyV); //this is an array I need to have: array(2) { [0]=> string(11) "Abc@abc.com" [1]=> string(11) "hello@g.com" }

最佳答案

fetch_assoc

while($row = $result->fetch_assoc()) {
  $rows[]=$row['EmailAddress'];
}

关于php - 如何使用 MySQLi 获取单列值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30761740/

相关文章:

php - 为什么 php 不能将引号转换为 mysql 的 html 实体?

php - 如何计算数组的元素?

php - 如何使 PHPStorm 在文件打开时折叠所有方法/函数?

php - 使用 html 表单导入 csv 数据...使用 PHP 脚本加载数据 infile 和 MySQL

mysql - SELECT * INTO <不存在的表>- MYSQL

javascript - 如何获取另一个数组中不存在的数组的所有项目

javascript - jQuery 在 while 循环数据中获取来自 AJAX 的属性值

javascript - 无法检索 Angularjs $scope 数组最后一个对象

arrays - 搜索值数组以在字典中查找键

php - 在 mysql 语句中插入变量时出错