Php mysqli_fetch_数组存在问题
我想将请求的结果放入这个数组,Php mysqli_fetch_数组存在问题,php,mysql,arrays,database,Php,Mysql,Arrays,Database,我想将请求的结果放入这个数组,$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_format
$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" ),
);}
您可以从简化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'] )
或
欢迎来到堆栈溢出。请您的问题提供更多有关出错原因的详细信息。@kamelberkani这是因为您的数组需要另一个a值。例如,你有
array(1,2,3,)
当它应该是array(1,2,3)
时,你的最后一个逗号导致了你的错误。谢谢你,它工作了,但是它没有得到形式1 2 3,而是在我的条形图中显示(r)一条错误消息:警告:C:\wamp\www\test1\etudiant.php第16行的非法字符串偏移量“formation”,ligne 16:$dataPoints1[]=array(“y”=>$row[“nbre1”],“label”=>row['formation'])@kamelberkani您可以编辑您的帖子并显示新代码吗?这是我的新代码$sql=“选择COUNT(id_etudiant)作为nbre1,formation.nom_formation作为suivre的formation加入suivre上的formation。n_formation=formation.n_formation按nom_formation分组”;$result=mysqli\u查询($link,$sql)//print_r($result=mysqli_query($link,$sql))$dataPoints1=数组();而($row=mysqli_fetch_array($result)){$dataPoints1[]=array(“y”=>$row[“nbre1”],“label”=>row['formation']);}
$dataPoints1[] = $row
}