2014-01-18 34 views
0

我使用PDO連接到MySQL錯誤建立一個數據庫連接PDO

include_once('connection_insert.php'); 
global $host, $dbname, $user, $pass; 
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
$DBH->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); ///*** for error handling 

function insert_terms(){ 
    global $DBH; 
    $STH = $DBH->prepare("INSERT IGNORE INTO table 
    (name,state) 
    value (?,?)"); 
    return $STH; 
    $DBH = Null;// Closing Connection 
} 

但我面對的錯誤:

Error establishing a database connection

我的問題是,在這種正確的方式使用global $DBH;和關閉連接$DBH = Null;// Closing Connection

編輯

connection_insert.php

$host = 'localhost'; 
$user = 'username'; 
$pass = 'password'; 
$dbname = 'db'; 

回答

2

沒有使用global變量是不好的做法(不看WordPress的&和其他使用全局變量)。結構(體系結構)應用程序越複雜 - 使用全局變量的後果越可怕。你可以找到很多爲什麼他們是邪惡的信息。相反,你可以使用Registry(例如)模式來訪問應用程序中的「全局」變量。總之,有很多選擇。

Сoncerningyour code。連接參數($host,$dbname,$user,$pass)是否定義?

另請參閱Global or Singleton for database connection?php.net

EDIT 實施例:

的index.php

require_once 'Database.php'; 

$DB = new Dababase(); 
$DB->prepare("INSERT IGNORE INTO table (name,state) value (:name,:state)"); 
$database->bind(':name', 'NAME'); 
$database->bind(':state', 'STATE'); 
$DB->execute(); 

CONFIG.PHP

define("DB_HOST", "localhost"); 
define("DB_USER", "username"); 
define("DB_PASS", "password"); 
define("DB_NAME", "database"); 

database.php中

require_once 'config.php'; 

class Database{ 
    private $host  = DB_HOST; 
    private $user  = DB_USER; 
    private $pass  = DB_PASS; 
    private $dbname = DB_NAME; 

    private $dbh; 
    private $error; 
    private $stmt; 

public function __construct(){ 
    // Set DSN 
    $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; 
    // Set options 
    $options = array(
     PDO::ATTR_PERSISTENT => true, 
     PDO::ATTR_ERRMODE  => PDO::ERRMODE_EXCEPTION 
    ); 
    // Create a new PDO instanace 
    try{ 
     $this->dbh = new PDO($dsn, $this->user, $this->pass, $options); 
    } 
    // Catch any errors 
    catch(PDOException $e){ 
     $this->error = $e->getMessage(); 
    } 
    } 

    public function prepare($query){ 
    $this->stmt = $this->dbh->prepare($query); 
    } 

    public function query($query){ 
    return $this->dbh->query($query); 
    } 

    public function bind($param, $value, $type = null){ 
    if (is_null($type)) { 
    switch (true) { 
     case is_int($value): 
      $type = PDO::PARAM_INT; 
      break; 
     case is_bool($value): 
      $type = PDO::PARAM_BOOL; 
      break; 
     case is_null($value): 
      $type = PDO::PARAM_NULL; 
      break; 
     default: 
      $type = PDO::PARAM_STR; 
     } 
    } 
    $this->stmt->bindValue($param, $value, $type); 
    } 

    public function execute(){ 
    return $this->stmt->execute(); 
    } 

    public function execute(){ 
    return $this->stmt->execute(); 
    } 

    public function fetchRows(){ 
    $this->execute(); 
    return $this->stmt->fetchAll(PDO::FETCH_ASSOC); 
    } 

    public function fetchRow(){ 
    $this->execute(); 
    return $this->stmt->fetch(PDO::FETCH_ASSOC); 
    } 

} 
+0

是的,我有include_once('connection_insert.php'); – Harinder

+0

顯示來自'connection_insert.php'的代碼 – voodoo417

+0

事實上,你需要拋出這段代碼並且正確寫入 – voodoo417