尝试运行mySQL PDO查询。不知道为什么我会收到致命的错误。我查过了其他帖子,但他们的答案似乎并没有解决我的问题。
脚本正好连接到数据库。用户名和密码是正确的,我已在下面的脚本中删除它们。
我的输出:
Connected to database
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'nobody'@'localhost' (using password: NO)' in /home/a/public_html/d/inc/header.php:34 Stack trace: #0 /home/a/public_html/d/inc/header.php(34): PDO->__construct('mysql:host=;dbn...', NULL, NULL) #1 /home/a/public_html/d/inc/header.php(43): testdb_connect() #2 /home/a/public_html/d/article.php(3): include('/home/a/p...') #3 {main} thrown in /home/a/public_html/d/inc/header.php on line 34
我的代码:
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'removed';
/*** mysql password ***/
$password = 'removed';
try {
function testdb_connect (){
$dbh = new PDO("mysql:host=$hostname;dbname=removed", $username, $password);
return ($dbh);
}
echo 'Connected to database';
}
catch(PDOException $e) {
echo $e->getMessage();
}
$dbh = testdb_connect ();
$id=$_GET[id];
echo 'dfsdfs '.$id;
var_dump($dbh);
$sql="SELECT * FROM 'radiologyArticles' WHERE 'id' = :id";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
?>
<section>
<header>
<h2><?php echo $row['articleTitle']; ?></h2>
<h3>A generic two column layout</h3>
</header>
<p>
<?php echo $row['articleBody']; ?>
</p>
</section>
<?php
}
// close the PDO connection
$link = null;
?>
答案 0 :(得分:2)
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'removed';
/*** mysql password ***/
$password = 'removed';
function testdb_connect ($hostname, $username, $password){
$dbh = new PDO("mysql:host=$hostname;dbname=removed", $username, $password);
return $dbh;
}
try {
$dbh = testdb_connect ($hostname, $username, $password);
echo 'Connected to database';
} catch(PDOException $e) {
echo $e->getMessage();
}