gpt4 book ai didi

php - 未捕获的异常 'PDOException',消息为“SQLSTATE[08P01]”

转载 作者:行者123 更新时间:2023-11-29 13:02:19 27 4
gpt4 key购买 nike

你好,我正在尝试为一个带有 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 个绑定(bind)参数,但您没有将任何值绑定(bind)到它。尝试这样的事情:

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 block 中。在您的示例中,其中唯一的内容是字符串变量的赋值,这是没有意义的,因为赋值字符串永远不会引发异常。

您还可以直接在调用execute 时将参数绑定(bind)到您的查询:

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

您还可以将一些不同的选项传递给 fetch method ,还有几个不同的 fetch methods你可以打电话,所以一定要检查那些并使用对你最有意义的任何东西。

关于php - 未捕获的异常 'PDOException',消息为“SQLSTATE[08P01]”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25960198/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com