mysqli\u fetch\u数组的问题

luaexgnf  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(387)

我想把我请求的结果放到这个数组中, $dataPoints1 = array ,但不起作用。
这是我的要求:

$sql = "
SELECT COUNT(id_etudiant)as nbre1 FROM suivre WHERE n_formation='1'
UNION 
SELECT COUNT(id_etudiant)FROM suivre WHERE n_formation='2'
UNION
SELECT COUNT(id_etudiant)FROM suivre 
WHERE n_formation='3'";

$result = mysqli_query($link,$sql);

while($row = mysqli_fetch_array($result)) { 
$dataPoints1 = array( 
    array("y" => ''. $row["nbre1"].'',"label" => "formation1" ),
    array("y" => ''. $row["nbre1"].'',"label" => "formation2" ),
    array("y" => ''. $row["nbre1"].'',"label" => "formation3" ),

);}
qlvxas9a

qlvxas9a1#

可以从简化sql开始

SELECT COUNT(id_etudiant)as nbre1, n_formation as formation FROM suivre GROUP BY n_formation;

首先在循环外声明数组,然后使用“[]”添加到下一个索引。
试试这个:

$dataPoints1 = array();
while($row = mysqli_fetch_array($result)) { 
    $dataPoints1[] = array("y" => $row["nbre1"], "label" => $row['formation'] )

$dataPoints1[] = $row
}

相关问题