我不明白,当创建这样一个对象,为什么会出现这样的错误: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 !!
你得到的错误是什么? –
写在标题:PDO连接错误:无效的数据源名称 – SoldierCorp
为什么你有'DNS'作为你的PDO构造函数的参数? –