我需要使用PDO方法进行“SELECT inside SELECT”,因为我需要来自第一个
的数据SELECT (`$result('nom')`)
为第二个。
我没有得到php错误,但页面重定向到“404 File Not Found”,SQL语法似乎正确。我错了什么?
这是我的代码:
<?php
include("../access.php");
session_start();
$date = "tableau_suivi_".date("d-m-Y").".csv";
$table_echo = array();
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename='.$date.'');
header('Pragma: no-cache');
header('Expires: 0');
$csv_file = fopen('php://output', 'w');
fputs($df, "\xEF\xBB\xBF" );
fwrite($csv_file, "Nom ; Prenom ; RSCA 1 ; RSCA 2 ; RSCA 3\n");
$sql='SELECT * FROM membre WHERE prof = 0';
$nb = $bdd->query($sql);
while($result = $nb->fetch()){
$sql1='SELECT nom_eval FROM rsca_valides WHERE nom_etu = "'.$result('nom').'" AND numero = 1';
$nb1 = $bdd->query($sql1);
if($res1 = $nb1->fetch()){
$eval1 = $res1('nom_eval');
}
$sql2='SELECT nom_eval FROM rsca_valides WHERE nom_etu = "'.$result('nom').'" AND numero = 2';
$nb2 = $bdd->query($sql2);
if($res2 = $nb2->fetch()){
$eval2 = $res2('nom_eval');
}
$sql3='SELECT nom_eval FROM rsca_valides WHERE nom_etu = "'.$result('nom').'" AND numero = 3';
$nb3 = $bdd->query($sql3);
if($res3 = $nb3->fetch()){
$eval3 = $res3('nom_eval');
}
$line = $result['nom'].";".$result['prenom'].";".$eval1.";".$eval2.";".$eval3."\n";
fwrite($csv_file, $line);
}
fclose($csv_file);
?>
没有$sql1
,$sql2
和$sql3
,它运行良好。我的目标是在每一行生成一个包含nom,prenom,$ res1('nom_eval'),$ res2('nom_eval')和$ res3('nom_eval')的CSV文件。
答案 0 :(得分:2)
$eval1 = $res1('nom_eval');
既不是数组也不是对象项。它应该是$eval1 = $res1['nom_eval'];
或$eval1 = $res1->nom_eval;
,具体取决于您的获取方法。
这适用于所有查询结果。例如:
$sql1='SELECT nom_eval FROM rsca_valides WHERE nom_etu = "'.$result['nom'].'" AND numero = 1';