I am trying to run a MySQL PDO query. I am not sure why I am getting a fatal error. I have checked other posts, but their answers don't seem to solve mine.
The script is connecting to the database fine. The username and password are correct and I have removed them in the script below.
My output:
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
My code:
<?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;
?>