【问题标题】:PDO SQL QUERY - Fill an array with results PHPPDO SQL QUERY - 用结果 PHP 填充数组
【发布时间】:2013-10-04 13:34:55
【问题描述】:

在我的 sql 查询之后,使用 PDO 和 fetch() 函数,我想填充一个二维数组,然后我可以在我的网站中显示或导出。

我在这个过程中遇到了一个奇怪的问题:前两行在他们自己的子数组中填满,但第三行没有,并继续填满第二个子数组。

我想实现这个:

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

#column 名称行

$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 数据,有什么建议吗?

【问题讨论】:

    标签: php sql arrays pdo 2d


    【解决方案1】:

    好吧,对不起,我是愚蠢的。

    我忘记取消设置子数组$ligne,添加它解决了问题!

    unset($ligne);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 2013-09-06
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      相关资源
      最近更新 更多