PDO异常错误

时间:2017-07-10 08:49:05

标签: php

有人可以帮我解决这些错误吗?

enter image description here

我的数据库中的表格如下:

enter image description here

这是我的代码:

<?php
  $bdd=new PDO('mysql:host=localhost;dbname=cmadatabase','root','@Rugby3390', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
  $reponse= $bdd->query('SELECT IR,Level,Type,ToBeAnalyzed,Supplier,
                                Consequences,Remarks,SUM(Level,Type,Supplier) AS sommeLvlTypeSup 
                            FROM andgate 
                            GROUP BY IR,Level,Type,ToBeAnalyzed,
                                    Supplier,Consequences,Remarks 
                            ORDER BY SUM(Level,Type,Supplier) ASC');
  while ($donnees = $reponse->fetch()) {
    echo '<p>'.$donnees['IR'].','.$donnees['Level'].','.
        $donnees['Type'].','.$donnees['ToBeAnalyzed'].','.
        $donnees['Supplier'].','.$donnees['Consequences'].','.
        $donnees['Remarks'].','.$donnees['sommeLvlTypeSup'].'<p>';
  }
?>

非常感谢

2 个答案:

答案 0 :(得分:0)

第一名 order by子句ORDER BY sommeLvlTypeSup ASC

中的别名

第二名: Typemysql keyword。所以尝试enclose backticks table name column名称

try
{
    $bdd=new PDO('mysql:host=localhost;dbname=cmadatabase','root','@Rugby3390');
    $bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $reponse= $bdd->query('SELECT `IR`,`Level`,`Type`,`ToBeAnalyzed`,
                                    `Supplier`,`Consequences`,`Remarks`,
                                    SUM(`Level`,`Type`,`Supplier`) AS sommeLvlTypeSup 
                            FROM `andgate` 
                            GROUP BY `IR`,`Level`,`Type`,`ToBeAnalyzed`,
                                     `Supplier`,`Consequences`,`Remarks` 
                            ORDER BY sommeLvlTypeSup  ASC');
    while ($donnees = $reponse->fetch()){
        echo '<p>'.$donnees['IR'].','.$donnees['Level'].','.$donnees['Type'].','.$donnees['ToBeAnalyzed'].','.$donnees['Supplier'].','.$donnees['Consequences'].','.$donnees['Remarks'].','.$donnees['sommeLvlTypeSup'].'<p>';
      }
}

catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage();}

答案 1 :(得分:0)

级别,键入这些是MySQL保留的关键字。你需要把它们妥善包围

    <?php
try{
      $bdd=new PDO('mysql:host=localhost;dbname=cmadatabase','root','@Rugby3390', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
        $reponse= $bdd->query('SELECT `IR`,`Level`,`Type`,`ToBeAnalyzed`,`Supplier`,`Consequences`,`Remarks`,SUM(`Level`,`Type`,`Supplier`) AS sommeLvlTypeSup FROM andgate GROUP BY `IR`,`Level`,`Type`,`ToBeAnalyzed`,`Supplier`,`Consequences`,`Remarks` ORDER BY SUM(`Level`,`Type`,`Supplier`) ASC');
    while ($donnees = $reponse->fetch())
    {
        echo '<p>'.$donnees['IR'].','.$donnees['Level'].','.$donnees['Type'].','.$donnees['ToBeAnalyzed'].','.$donnees['Supplier'].','.$donnees['Consequences'].','.$donnees['Remarks'].','.$donnees['sommeLvlTypeSup'].'<p>';
    }
} catch(PDOExpection $px) {
    echo $px->getMessage();
}
    ?>

参考:https://dev.mysql.com/doc/refman/5.7/en/keywords.html