PDO准备的语句正确吗?
我正在尝试首次使用一些PDO准备语句来防止SQL注入。 我对SQL很陌生,所以准备好的语句对我来说非常困惑。 你认为我准备好的SQL语句是正确的吗?
<?php
    if ( $_SERVER["REQUEST_METHOD"] == 'POST' ) {
    $suche = htmlspecialchars($_POST['suche']);
    $stmt->bindParam(':suche', $suche);
    if (!empty($suche)) {    
    $sql = new rex_sql;
    $sql->debugsql = 0;
    $stmt = $sql->prepare("SELECT * FROM rex_downloads WHERE dateiname LIKE '%:suche%' OR projektnummer LIKE '%:suche%' OR teilnehmer LIKE '%:suche%'");
    $stmt->execute();
    if ($sql->getRows() >= 1) {
    for($i = 1; $i <= $sql->getRows(); $i++, $sql->next())
    {
        $dateiname_suche = $sql->getValue("dateiname");
        $datei_projektnummer_suche = $sql->getValue("projektnummer");
        $teilnehmer_suche = $sql->getValue("teilnehmer");
        $dateidatum_suche = date("d.m.Y",strtotime($sql->getValue("dateidatum")));
        $dateizeit_suche = date("H.i",strtotime($sql->getValue("dateidatum")));
        $datei_projektseite_suche = $sql->getValue("projektseite");
        $suche_download_ausgabe .= '<li><a href="index.php?article_id='.$datei_projektseite_suche.'"></a><i class="fa fa-file-o"></i>'.$dateiname_suche.'<ul><li><i class="fa fa-calendar"></i>'.$dateidatum_suche.' um '.$dateizeit_suche.' Uhr</li><li><i class="fa fa-circle"></i>'.$datei_projektnummer_suche.'</li><li><i class="fa fa-user"></i>'.$teilnehmer_suche.'</li></ul></li>';   
    }  
    }   
    }   
    }        
    ?> 
这是我的“旧”SQL代码(没有准备好的语句):
<?php
    if ( $_SERVER["REQUEST_METHOD"] == 'POST' ) {
    $suche = htmlspecialchars($_POST['suche']);
    if (!empty($suche)) {    
    $sql = new rex_sql;
    $sql->debugsql = 0;
    $sql->setQuery("SELECT * FROM rex_downloads WHERE dateiname LIKE '%$suche%' OR projektnummer LIKE '%$suche%' OR teilnehmer LIKE '%$suche%'");
    if ($sql->getRows() >= 1) {
    for($i = 1; $i <= $sql->getRows(); $i++, $sql->next())
    {
        $dateiname_suche = $sql->getValue("dateiname");
        $datei_projektnummer_suche = $sql->getValue("projektnummer");
        $teilnehmer_suche = $sql->getValue("teilnehmer");
        $dateidatum_suche = date("d.m.Y",strtotime($sql->getValue("dateidatum")));
        $dateizeit_suche = date("H.i",strtotime($sql->getValue("dateidatum")));
        $datei_projektseite_suche = $sql->getValue("projektseite");
        $suche_download_ausgabe .= '<li><a href="index.php?article_id='.$datei_projektseite_suche.'"></a><i class="fa fa-file-o"></i>'.$dateiname_suche.'<ul><li><i class="fa fa-calendar"></i>'.$dateidatum_suche.' um '.$dateizeit_suche.' Uhr</li><li><i class="fa fa-circle"></i>'.$datei_projektnummer_suche.'</li><li><i class="fa fa-user"></i>'.$teilnehmer_suche.'</li></ul></li>';   
    }  
    }   
    }   
    }        
    ?> 
谢谢!
  您的占位符不应被引用。  这使它成为一个文字字符串,而不是占位符。  我也不知道getRows()和getValue是什么。  试试这个.. 
if ( $_SERVER["REQUEST_METHOD"] == 'POST' ) {
    $suche = '%' . htmlspecialchars($_POST['suche']) . '%';
    $stmt = $sql->prepare("SELECT * FROM rex_downloads WHERE dateiname LIKE ? OR projektnummer LIKE ? OR teilnehmer LIKE ?");
    $stmt->execute(array($suche, $suche, $suche));
    if ($stmt->rowCount() > 0) {
        $suche_download_ausgabe = '';
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $dateiname_suche = $row['dateiname'];
            $datei_projektnummer_suche = $row['projektnummer'];
            $teilnehmer_suche = $row['teilnehmer'];
            $dateidatum_suche = date("d.m.Y",strtotime($row['dateidatum']));
            $dateizeit_suche = date("H.i",strtotime($row['dateidatum']));
            $datei_projektseite_suche = $row['projektseite'];
            $suche_download_ausgabe .= '<li><a href="index.php?article_id='.$datei_projektseite_suche.'"></a><i class="fa fa-file-o"></i>'.$dateiname_suche.'<ul><li><i class="fa fa-calendar"></i>'.$dateidatum_suche.' um '.$dateizeit_suche.' Uhr</li><li><i class="fa fa-circle"></i>'.$datei_projektnummer_suche.'</li><li><i class="fa fa-user"></i>'.$teilnehmer_suche.'</li></ul></li>';
        }
    } else {
        echo 'No results';
    }
}
  这个?  s是用户值将被插入的占位符。  rowCount是一个PDO函数,用于查看查询返回的行数。  fetch是另一个PDO函数来拉行。  PHP站点上有这些函数的引用和写法。 
  我也不确定你应该在用户输入上运行htmlspecialchars 。  数据库中的数据是否在插入时以这种方式转换? 
参考文献:
  http://php.net/manual/en/pdostatement.rowcount.php 
  http://php.net/manual/en/pdostatement.fetch.php 
  http://php.net/manual/en/pdo.prepared-statements.php 
