Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

mysql - PHP PDO how to run a multiple query request?

Hello I'm trying to execute this SQL request in PHP with PDOStatement:

CREATE TEMPORARY TABLE r1
SELECT CONCAT(MONTH(Heure_deb),'/',DAY(Heure_deb)) as 'Date',
                      Heure_deb,
                      Operateur.Id_op ,
                      Nom_op ,
                      Prenom_op,
                      Nom_act ,
                      TIME(Heure_deb) as heure,
                      Commentaire,
                      Type
                      FROM Operateur, Pointage, Activite
                      WHERE Operateur.Id_op = Pointage.Id_op
                      AND Activite.Id_act = Pointage.Id_act
                      ORDER BY date, Id_op, heure
;
Create temporary table r2
SELECT a.Id_op, a.Heure_deb, MIN(b.heure_deb) as fin, TIMEDIFF(b.Heure_deb, a.Heure_deb) as Time_Difference, ROUND(HOUR(TIMEDIFF(b.Heure_deb, a.Heure_deb)) + MINUTE(TIMEDIFF(b.Heure_deb, a.Heure_deb))/60,2) as Decimal_duree
FROM Pointage a
LEFT JOIN Pointage b ON a.Id_op = b.Id_op
WHERE a.heure_deb < b.heure_deb
Group by a.Id_op, a.Heure_deb
;
select CONCAT(MONTH(r1.Heure_deb),'/',DAY(r1.Heure_deb)) as 'Date',
                      TIME(r1.Heure_deb) as heure,
                      r1.Id_op ,
                      Nom_op ,
                      Prenom_op,
                      Nom_act ,
                      Commentaire,
                      Type,
                      Time_Difference,
                      Decimal_duree
from r1
LEFT JOIN r2 ON r1.Id_op = r2.Id_op and r1.heure_deb = r2.heure_deb
Order by Id_op, Date , heure

This request return this in phpMyAdmin Request

But in PHP the fetch doesn't works, return false but any error code. PHP Code:

  $result=$objPdo->prepare($requete);
  $result->execute();
  $err = $result->errorInfo();
  if(!empty($err[2])){
  echo"<script type='text/javascript'>alert('Erreur SQL :".$err[2]."');</script>";
  }else{
  while($row=$result->fetch()){
    echo"<tr>
              <td>".$row['Date']."</td>
              <td>".$row['heure']."</td>
              <td>".$row['Nom_op']."</td>
              <td>".$row['Prenom_op']."</td>
              <td>".$row['Type']."</td>
              <td>".$row['Time_Difference']."</td>
              <td>".$row['Decimal_duree']."</td>
              <td>".$row['Commentaire']."</td>
          </tr>";
  }
  echo"</table></div>";
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are three queries in this request and therefore you have to run them in three calls, not one:

$objPdo->query("CREATE TEMPORARY TABLE r1
SELECT CONCAT(MONTH(Heure_deb),'/',DAY(Heure_deb)) as 'Date',
                      Heure_deb,
                      Operateur.Id_op ,
                      Nom_op ,
                      Prenom_op,
                      Nom_act ,
                      TIME(Heure_deb) as heure,
                      Commentaire,
                      Type
                      FROM Operateur, Pointage, Activite
                      WHERE Operateur.Id_op = Pointage.Id_op
                      AND Activite.Id_act = Pointage.Id_act
                      ORDER BY date, Id_op, heure
;";

$objPdo->query("Create temporary table r2
SELECT a.Id_op, a.Heure_deb, MIN(b.heure_deb) as fin, TIMEDIFF(b.Heure_deb, a.Heure_deb) as Time_Difference, ROUND(HOUR(TIMEDIFF(b.Heure_deb, a.Heure_deb)) + MINUTE(TIMEDIFF(b.Heure_deb, a.Heure_deb))/60,2) as Decimal_duree
FROM Pointage a
LEFT JOIN Pointage b ON a.Id_op = b.Id_op
WHERE a.heure_deb < b.heure_deb
Group by a.Id_op, a.Heure_deb
;";

$result = $objPdo->query("select CONCAT(MONTH(r1.Heure_deb),'/',DAY(r1.Heure_deb)) as 'Date',
                      TIME(r1.Heure_deb) as heure,
                      r1.Id_op ,
                      Nom_op ,
                      Prenom_op,
                      Nom_act ,
                      Commentaire,
                      Type,
                      Time_Difference,
                      Decimal_duree
from r1
LEFT JOIN r2 ON r1.Id_op = r2.Id_op and r1.heure_deb = r2.heure_deb
Order by Id_op, Date , heure";

while($row=$result->fetch()){
    echo"<tr>
          <td>".$row['Date']."</td>
          <td>".$row['heure']."</td>
          <td>".$row['Nom_op']."</td>
          <td>".$row['Prenom_op']."</td>
          <td>".$row['Type']."</td>
          <td>".$row['Time_Difference']."</td>
          <td>".$row['Decimal_duree']."</td>
          <td>".$row['Commentaire']."</td>
      </tr>";
}
echo"</table></div>";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...