1

次のようなオブジェクトを作成するときに、なぜこのエラーが表示されるのかわかりません。

エラーは次の行の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()関数または他の関数に接続して、データを取得したいのですが、それだけです。

説明したと思います。

よろしく!

4

2 に答える 2

1

無効なデータソース名などのエラーは、次のことを示している可能性があります。

  1. DSN構文が正しくありません(あなたの場合は正しいように見えます)。
  2. DSNパラメーターが正しくありません
    1. ホストはローカルホストではない可能性があります(mysql構成ではTCP経由またはローカルホスト自体からの接続が許可されていない可能性がありますが、ローカルホストからのTCP接続を許可することがデフォルトであるため、疑わしいです)。
    2. データベース名が正しくない可能性があります(コード内にあるためexample)。exampleという名前のデータベースを作成していない可能性があります。
于 2012-09-01T00:47:05.900 に答える
1

解決しました!私だけがこの行を追加する必要がありました

require_once 'include/config.php'; 

その上

require_once 'include/DB_Functions.php'; 

と動作しますが、なぜ私は理解していませんか?

DB_Connect.phpではこの行が同じだからです。

于 2012-09-02T01:12:41.980 に答える