我正在尝试运行 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
Run Code Online (Sandbox Code Playgroud)
我的代码:
<?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;
?>
Run Code Online (Sandbox Code Playgroud)
/*** 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();
}
Run Code Online (Sandbox Code Playgroud)