在PHP和MySQL中以数组格式获取数据

时间:2019-05-02 11:57:38

标签: php mysql arrays

以数组格式获取数据:

$value = array ("ABC","XYZ","OPQ");

$query = "SELECT * FROM  designation_master";
$result = mysqli_query($mysqli, $query);
while ($row = mysqli_fetch_array($result)) {
    $designation= $row["designation"]; 
}

需要的结果:

$value = array ("ABC","XYZ","OPQ");

2 个答案:

答案 0 :(得分:1)

获取designation列,然后将其推入语法为$array[] = $value的数组中。

$designation = [];
$query = "SELECT designation FROM  designation_master";
$result = mysqli_query($mysqli, $query);
while ($row = mysqli_fetch_array($result)) {
    $designation[] = $row["designation"]; 
}
print_r($designation);

答案 1 :(得分:1)

如果“ ABC”,“ XYZ”,“ OPQ”是相同的字段[designation],则可以将$ designation转换为数组。

$query = "SELECT * FROM  designation_master";
$result = mysqli_query($mysqli, $query);
$designation = array();
while ($row = mysqli_fetch_array($result))
{  
    $designation[] = $row["designation"]; 
}