php 未捕获的异常“PDOException”消息“无效数据源名称”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19740829/
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 19:56:55  来源:igfitidea点击:

Uncaught exception 'PDOException' message 'invalid data source name'

phpmysqldatabaseexceptionpdo

提问by Lisa

I′m trying to connect to my database with PDO and show some blogposts on a page. However I′m getting this error message:

我正在尝试使用 PDO 连接到我的数据库并在页面上显示一些博客文章。但是我收到此错误消息:

Fatal error: Uncaught exception 'PDOException' with message 'invalid data source name' in index.php on line 61...

致命错误:第 61 行的 index.php 中未捕获的异常“PDOException”和消息“无效的数据源名称”...

I′ve been searching for help but really can′t figure out what is wrong so if anyone have any idea it is much appreciated!

我一直在寻求帮助,但真的无法弄清楚出了什么问题,所以如果有人有任何想法,我将不胜感激!

I have a separate connect.inc.php file which is included in the index.php file.

我有一个单独的 connect.inc.php 文件,它包含在 index.php 文件中。

This is the connect.inc.php file:

这是 connect.inc.php 文件:

<?php
class DB extends PDO
{
function database_connection() {
   $db_host = "localhost";
   $db_name = "blogdata";
   $db_user = "username";
   $db_pass = "password";
   try {
   global $db_host, $db_name, $db_user, $db_pass;
   $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
   }
   catch(PDOException $e) {
   die( 'Query failed: ' . $e->getMessage() );
}
}
}
?>

And this is the section in the index.php file which is pointed out in the error message:

这是错误消息中指出的 index.php 文件中的部分:

<?php
    require 'connect.inc.php';  
    $db = new DB('blogdata');

    $query = "SELECT * FROM blogposts";
    if ($result = $db->query($query)) {
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        echo ' 
            <section id="content">
            <article class="post_title"><h3> ', $row['title'],' </h3></article>
            <article class="post_message"> ', nl2br ($row['message']),' </article>
            <article class="post_time"> ',$row['time'],' </article>
            </section>
            ';
        }
    } ;
    ?>

回答by Your Common Sense

Gotcha.

明白了。

For some reason you are extendingyour class from PDO. So, your 'blogdata' is taken as a DSN.

出于某种原因,您正在从 PDO扩展您的类。因此,您的“博客数据”被视为 DSN。

Just get rid of your DB class and use raw PDO

只需摆脱您的 DB 类并使用原始 PDO

connect.inc.php:

connect.inc.php:

<?php 
$db_host = "localhost";
$db_name = "blogdata";
$db_user = "username";
$db_pass = "password";
$db = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

index.php:

索引.php:

<?php
require 'connect.inc.php'; 

$query = "SELECT * FROM blogposts";
$result = $db->query($query);
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {

回答by gedq

Why are you declaring your DB variables global after initialising them? I can't test it but if memory serves that'll pull in existing values from global, overwriting the ones you've just declared.

为什么在初始化后将数据库变量声明为全局变量?我无法测试它,但如果内存可用,它将从全局中提取现有值,覆盖您刚刚声明的值。

I disagree with not using an inherited class though - being able to write a custom constructor means you can set all modes and attributes once and then just call the custom constructor to have it done automatically. Much cleaner than having to do it every time.

我不同意不使用继承的类 - 能够编写自定义构造函数意味着您可以一次设置所有模式和属性,然后只需调用自定义构造函数即可自动完成。比每次都必须这样做要干净得多。

回答by Chihab

For future references, you could keep the DB class and refer to the following solution that hasn't much to do with the php class:

为了将来参考,您可以保留 DB 类并参考以下与 php 类没有太大关系的解决方案:

enter link description here

在此处输入链接描述