【问题标题】:PDO: Why does this cause an error?PDO:为什么这会导致错误?
【发布时间】:2012-09-01 00:20:02
【问题描述】:

我不明白为什么在创建这样的对象时会出现此错误:

错误在 index.php 这一行:

$dbPerfiles = new DB_Functions();

错误是这样的:

PDO Connection error: invalid data source name

config.php

<?php
define ("DB_USER","root");
define ("DB_PASS","root");
define ("DNS","mysql:host=localhost;dbname=example");
?>

DB_Connect.php

<?php
require_once 'config.php';
class DB_Connect {
    private static $_instance;

    //Connecting to database
    public function &pdo_connect() {    
        if(!self::$_instance) {
            try{
                self::$_instance = new PDO(DNS,DB_USER, DB_PASS);
                self::$_instance->setAttribute(PDO::ATTR_PERSISTENT, true);
                self::$_instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $ex) {
                die("PDO Connection error: ".$ex->getMessage()."<br/>");
            }
        }
        return self::$_instance;
    }

    private function __construct() {
    }

    private function __clone() {
    }
}
?>

DB_Functions.php

<?php
    session_start();
    require_once 'DB_Connect.php';

    class DB_Functions extends DB_Connect{

        private $dbConnect = "";

        public function __construct() {
            $this->dbConnect = $this->pdo_connect();
        }

        public function __destruct() {
            $this->dbConnect = null;
        }

        public function getDetails() {
            try {
                //sql statement

            } catch (PDOException $e) {
                echo "Error: ".$e->getMessage()."<br/>";
                return false;
            }
        }
    }
?>

index.php

<?php 
session_start();
$max_time = 1800;
$current = time();
if(!isset($_SESSION['clientmac']['un']) ) { 
    $_SESSION['clientmac']['un'] == "";
    header('Location: index.php');
} else {
    if (!isset($_SESSION['timeLogin'])){
        $_SESSION['clientmac']['tl'] = time();
    } else {
        $session_life = $current - $_SESSION['clientmac']['tl'];    

        if ($session_life > $max_time) {
            header('Location: include/logout.php');
        } else {
            $_SESSION['clientmac']['tl'] = time();
        }
    }

    require_once 'include/DB_Functions.php';
    $dbPerfiles = new DB_Functions();  //With this line shows the error

    //code to connect to getDetails() function in DB_Functions.php and
    //retrieve some data.

?>
<!doctype html>
<html lang=en>
<!--
CODE HTML
-->
</html>
<?php
}
?>

我想连接到 DB_Functions 并连接到 getDetails() 函数或其他函数 在此文件中并检索数据.. 仅此而已!

希望我已经解释过了。

问候!!

【问题讨论】:

  • 您遇到的错误是什么?
  • 标题中写着:PDO连接错误:数据源名称无效
  • 为什么你有一个DNS 作为你的 PDO 构造函数的参数?
  • @SoldierCorp 哦!对不起。现在更清楚了! ;)
  • @arxanas 这是数据源名称、DSN、看起来像mysql:host=localhost;dbname=my_database的连接字符串的拼写错误。

标签: php pdo


【解决方案1】:

数据源名称无效等错误可能指:

  1. DSN 语法不正确(在您的情况下看起来正确);
  2. DSN 参数不正确
    1. 主机可能不是 localhost(您的 mysql 配置可能不允许通过 TCP 或来自 localhost 自身的连接,但我对此表示怀疑,因为默认允许来自 localhost 的 TCP 连接);
    2. 数据库名称可能不正确(因为在您的代码中是 example)让我觉得您可能没有创建名为 example 的数据库;

【讨论】:

  • 在 dbname 中,我仅将“example”作为示例,但我的 dbname 是 mac_system。
  • 并尊重“本地主机”..我只是放了一部分代码,但我有很多功能并且工作正常!但只有在 index.php 中 import 或 require DB_Functions 时,才会显示该错误!
  • 那么您应该为实际存在的数据库使用真实的数据库名称。 PDO 构造函数尝试连接到 MySQL 数据库,这意味着数据库不存在它无法连接并触发错误。
  • 解决了!只有我只需要添加这一行 require_once 'include/config.php';以上 require_once 'include/DB_Functions.php';并且有效,但我不明白为什么?因为在 DB_Connect.php 中是同一行..
【解决方案2】:

解决了!只有我只需要添加这一行

require_once 'include/config.php'; 

上面

require_once 'include/DB_Functions.php'; 

并且有效,但我不明白为什么?

因为在 DB_Connect.php 中与这一行相同。

【讨论】:

    猜你喜欢
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 2018-10-11
    • 2011-03-16
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多