2014-09-21 137 views
1

你好,所以我想爲一個網站whith PDO和PSQL做一個search.php文件,我是種新的所有這一切,所以我得到這個錯誤未知的異常'PDOException'與消息'SQLSTATE [08P01]

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[08P01]: <<Unknown error>>: 7 ERROR: bind message supplies 0 parameters, but prepared statement "pdo_stmt_00000001" requires 1' in C:\Program Files (x86)\PostgreSQL\EnterpriseDB-ApachePHP\apache\www\Library\search.php:45 Stack trace: #0 C:\Program Files (x86)\PostgreSQL\EnterpriseDB-ApachePHP\apache\www\Library\search.php(45): PDO->query('SELECT * FROM b...') #1 {main} thrown in C:\Program Files (x86)\PostgreSQL\EnterpriseDB-ApachePHP\apache\www\Library\search.php on line 45 

,我不明白爲什麼,我必須做什麼......如果有人可以幫助我 這沒有IDEIA是的search.php文件:

<?php 
    error_reporting(E_ALL); 
    ini_set('display_errors', 1); 
    include '/database/dbc.php'; 
    //$sesion_type = $_GET['search']; 
    $dbc = dbc(); 

     $search = $_POST['q'];  
?> 
<!DOCTYPE html> 
<html> 
    <head> 
      <title>Library</title> 
      <link rel="stylesheet" type="text/css" href="css/style.css"/> 
      <link rel="icon" href="images/biblioteca1.jpg"/> 
    </head> 

    <body> 
     <div id="container"> 
      <div id="header"> 
       <h2>Search</h2> 
      </div> 
      <div id="menu"> 
       <ul> 
        <li><a href="index.php">Home</a></li> 
       </ul> 
       <div id="tfheader"> 
        <form id="tfnewsearch" method="get" action="http://localhost:8080/Library/search.php"> 
        <input type="text" class="tftextinput" name="q" size="21" maxlength="120"><input type="submit" value="search" class="tfbutton"> 
        </form> 
       <div class="tfclear"></div> 
       </div> 

      </div> 
      <div id="content"> 
      <?php 
      try 
      { 
       $quer1 = "SELECT * FROM books WHERE title LIKE :search OR author LIKE :search OR genre LIKE :search OR editor LIKE :search"; 
      } 
      catch(PDOException $e) 
      { 
      echo $e->getMessage(); 
      } 
      foreach($dbc->query($quer1) as $row) 
      { 
      ?> 
       <td><?php echo($row['title']);?></td> 
       <td><?php echo($row['author']);?></td> 
       <td><?php echo($row['editor']);?></td> 
       <td><?php echo($row['price']);?></td> 
       <td><?php echo($row['genre']);?></td> 
       <td><?php echo($row['bookid']);?></td> 
      <?php 
      } 
      ?> 
      </div> 
      <div id="footer"> 
      </div> 
     </div> 
    </body> 
</html> 

回答

2

該錯誤消息告訴你到底是什麼問題。你的查詢有一個綁定參數,但你沒有綁定任何值。嘗試是這樣的:

try { 
    $quer1 = "SELECT * FROM books WHERE title LIKE :search OR author LIKE :search OR genre LIKE :search OR editor LIKE :search"; 
    $sth = $dbc->prepare($quer1); 
    $sth->bindParam(':search', $search); 
    $sth->execute(); 

    while($row = $sth->fetch()) { 
     // ... 
    } 

} catch(PDOException $e) { 
    echo $e->getMessage(); 
} 

又見了bindParam手工錄入的例子。請注意,我還在try區塊內封裝了整個操作。在你的例子中,它唯一的內容是一個字符串變量的分配,這是毫無意義的,因爲分配一個字符串永遠不會拋出異常。

您也可以直接調用裏面的參數綁定到你的查詢execute

$sth->execute(array(':search' => $search)); 

還有幾個不同的選項,你可以在傳遞給fetch method,並有一對夫婦不同fetch methods的你可以電話,所以一定要檢查出來,並使用最有意義的東西。

+0

它的工作原理!謝謝你:D – Alex 2014-09-21 14:47:51

+0

np,很高興提供幫助 – 2014-09-21 14:48:44

相關問題