簡體   English   中英

致命錯誤:調用未定義的方法PDO :: error()

[英]Fatal error: Call to undefined method PDO::error()

顯然,這對我的老師有效,但對我來說,它給出錯誤致命錯誤:調用未定義的方法PDO :: error()

有問題的代碼:

<!DOCTYPE html>
<html>
<head>
<title>Mini-Chat</title>
<meta charset="UTF-8">
<style>
form
{
text-align: center;
}
</style>
<body>

<form action="minichat-post.php" method ="post">
<p>
    <label for="username">Username</label> : <input type="text" name="username" id="username"/><br>
    <label for="message">Message</label> : <input type="text" name="message" id="message"/><br>
    <input type="submit" value="Send"/>
</p>
</form>
<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '');
}
catch(Exception $e)
{
die('Error :'.$e->getMessage());
}
$response = $bdd->query('SELECT username, message FROM minichat ORDER BY id DESC LIMIT 0, 10');
while ($data = $response->fetch())
{
echo '<p><strong>' . htmlspecialchars($data['username']) . '</strong> : ' . htmlspecialchars($data['message']) . '</p>';
}
$response->closeCursor();
?>
</body>
</html>

更具體地說,它給了我錯誤行31,這是while循環:

while ($data = $response->fetch())

在這里瘋了,因為它已經工作了一次,做了很小的修改,但是現在我似乎找不到要在哪里做。

編輯:錯誤現在消失了,但是它將不會在發送中顯示任何消息,也不會在數據庫中保存任何消息。 這里的PHP文件:

<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=minichat', 'root', '');
}
catch(Exception $e)
{
die('Error :'.$e->getMessage());
}

$req = $bdd->prepare('INSERT INTO minichat (username, message VALUES (?, ?)');
$req->execute(array($_POST['username'], $_POST['message']));
header('Location: ./mini-chat[conflit].php');
?>

加上這個

fetch(PDO::FETCH_ASSOC);

在這里

fetch()

PDOStatement :: fetch

同時檢查帖子

<?php
if(isset($_POST['username']) && isset($_POST['message'])) {
   try
   {
    $bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '');
    }
    catch(Exception $e)
    {
    die('Error :'.$e->getMessage());
    }
    $response = $bdd->query('SELECT username, message FROM minichat ORDER BY id DESC LIMIT 0, 10');
    while ($data = $response->fetch())
    {
    echo '<p><strong>' . htmlspecialchars($data['username']) . '</strong> : ' . htmlspecialchars($data['message']) . '</p>';
    }
    $response->closeCursor();
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM