dongyou6795 2014-05-25 12:57
浏览 50
已采纳

致命错误:带有消息[重复]的未捕获异常'PDOException'

Trying to run a mySQL PDO query. 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;
?>
</div>
  • 写回答

1条回答 默认 最新

  • duanrang3357 2014-05-25 13:04
    关注
    /*** 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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等