未捕获的异常' PDOException'消息' SQLSTATE [08P01]

时间:2014-09-21 14:24:59

标签: php postgresql pdo psql

您好,我正在尝试为PDO和PSQL的网站制作一个search.php文件,我对所有这些都是新手,所以我收到此错误

    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

我无法理解为什么,我不知道该做什么......如果有人能帮助我的话 这是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>

1 个答案:

答案 0 :(得分:2)

错误消息告诉您确切的问题所在。您的查询有1个绑定参数,但您没有绑定任何值。尝试这样的事情:

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你可以打电话,所以一定要查看一下并使用对你最有意义的东西。