php 致命错误:带有消息的未捕获异常“PDOException”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23855518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:00:33  来源:igfitidea点击:

Fatal error: Uncaught exception 'PDOException' with message

phpmysqlpdo

提问by bobafart

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.

我正在尝试运行 MySQL PDO 查询。我不确定为什么会出现致命错误。我检查了其他帖子,但他们的答案似乎没有解决我的问题。

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;
        ?>

回答by Mark Baker

/*** 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();
}