如何使用 MySQLi 获取单列值? [英] How to get single column values using MySQLi?

查看:39
本文介绍了如何使用 MySQLi 获取单列值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get list or emails in a one-dimensional array from MySQL database. However, I am getting multidimensional array, rather then array:

$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" }

推荐答案

do fetch_assoc

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

这篇关于如何使用 MySQLi 获取单列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆