[英]PDO SQL QUERY - Fill an array with results PHP
在我的SQL查詢之后,使用PDO和fetch()函數,我想填充一個2D數組,然后可以在我的網站上顯示或導出。
我在這個過程中遇到了一個奇怪的問題:前兩行在自己的子數組中可以很好地填充,但是第三行沒有,並繼續填充第二個子數組。
我想實現這一目標:
$tableau_pour_lexport= array (
0 => "filename",
1 => array (colomn name1 .. colomn name n )
2=> array (value 1... value n)
3 => array (value 1 .. value n)
.... );
但是我得到這樣的東西:
$tableau_pour_lexport= array (
0 => "filename",
1 => array (colomn name1 .. colomn name n )
2=> array (value 1... value n)
3 => array (value 1 .. value n, value 1....value n, value 1... value n)
);
這是我的代碼:
<?php
$sql_query="SELECT id_dossier, type_sortie, date_incident, no_train, commentaire, conclusion_cause, retard_min, etat, no_rame1 FROM sortie_prevue
UNION
SELECT id_dossier, type_sortie, date_incident, no_train, commentaire, etat, conclusion_cause, retard_min, no_rame1 FROM sortie_non_prevue
ORDER BY date_incident LIMIT 3";
$reponse = $bdd->query("$sql_query");
#文件名行
$tableau_pour_lexport[0]="titre du fichier";
#列名稱行
$premiere_ligne= $reponse->fetch(PDO::FETCH_ASSOC);
foreach ($premiere_ligne as $cle=>$element1 )
{
$colonnes[]=$cle;
}
$tableau_pour_lexport[1]=$colonnes;
#value同時應填滿其余部分
while ($donnees = $reponse->fetch(PDO::FETCH_NUM))
{
foreach ($donnees as $valeur )
{
$ligne[]=$valeur;
}
$tableau_pour_lexport[]=$ligne;
}
print_r($tableau_pour_lexport);
$reponse->closeCursor(); // Termine le traitement de la requête
?>
任何幫助都將受到歡迎! 有什么建議可以解決我的sql數據嗎?
好的,抱歉,我很愚蠢。
我忘了取消設置子數組$ ligne,通過添加來解決問題!
unset($ligne);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.