2012-09-01 228 views
1

我不明白,当创建这样一个对象,为什么会出现这样的错误:PDO:为什么会导致错误?

的错误是在这条线的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 connect()函数或其他功能 在这个文件中检索数据..只有这个!

我希望我解释过。

Regards !!

+0

你得到的错误是什么? –

+0

写在标题:PDO连接错误:无效的数据源名称 – SoldierCorp

+0

为什么你有'DNS'作为你的PDO构造函数的参数? –

回答

1

解决它!只有我不得不添加上述

require_once 'include/DB_Functions.php'; 

和工程这一行

require_once 'include/config.php'; 

,但我不明白为什么?

因为在DB_Connect.php中这条线是相同的。

1

的错误,如无效数据源名称可参考:

  1. DSN的语法不正确而(在你的情况下,它看起来是正确的);
  2. 的DSN参数是不正确
    1. 主机可以不被本地主机(你的MySQL配置可能不允许经由TCP或从本地主机intself连接,但我怀疑,因为允许来自本地主机的TCP连接是默认值);
    2. 数据库名称可能不正确(因为在你的代码中是example)让我觉得你可能没有创建一个名为example的数据库;
+0

那么在dbname中,我只把“example”放在例子中,但是我的dbname是mac_system。 – SoldierCorp

+0

尊重“本地主机”..我只是把代码的一部分,但我有很多功能,并正常工作!但是只有在导入或需要index.php中的DB_Functions时才显示错误! – SoldierCorp

+0

那么你应该为实际存在的数据库使用真实的数据库名称。 PDO构造函数试图连接到MySQL数据库,这意味着数据库不存在,它不能连接并触发错误。 –

相关问题