2014-04-30 120 views
1

驗證以及標題說得夠多我猜! 我的數據庫連接總是失敗的核查出於某種原因...MySQL數據庫無法使用php pdo

<?php 
//main.php\\ 
require('database_pdo.php'); 
$con = new Database($dbhost,$dbusername,$dbpassword,$dbname); 
if($con){ 
    header("Refresh: 5; url=index.php"); 
    $message = '<font color="LIME"><center>The page will refresh within 5 seconds to  the next step!<br /><img src="./theme/main/CountDown.gif"/></center></font>'; 
}else{ 
    $message = '<font color="RED"><center>Database connection failed!<br />Please try again and/or check your connection info!<br />Error Given:<br />'.$dberror.'</center></font>'; 
} 

//database_pdo.php\\ 
class Database extends PDO 
    { 
      private $db; 
      public function Database($host, $user, $pass, $db) { 
        try { 
          $this->db = new PDO("mysql:dbname=".$db.";host=".$host.";", $user, $pass);    
        } catch(PDOEXCEPTION $e) { 
          $dberror = 'An error has occurred! [Code: '.$e->getCode().']!<br/>More info: ['.$e->getMessage().']!'; 
        } 
      } 
      public function runQuery($query) { 
        try{ 
          return $this->db->query($query); 
        } catch(PDOEXCEPTION $e) { 
          $dberror = 'An error has occurred! [Code: '.$e->getCode().']!<br/>More info: ['.$e->getMessage().']!'; 
        }    
      } 
    } 
?> 

任何人可以幫助我,它會被apperciated! 這是一個非常惱人的錯誤,因此必須儘快 我問它在聊天,但沒有人知道所以這可能會幫助我更好地

回答

1

試試這個

<?php 
//main.php\\ 
global $dberror; 
$dberror = ""; 
require('database_pdo.php'); 
$con = new Database($dbhost,$dbusername,$dbpassword,$dbname); 
if($con->db){ 
    header("Refresh: 5; url=index.php"); 
    $message = '<font color="LIME"><center>The page will refresh within 5 seconds to the next step!<br /><img src="./theme/main/CountDown.gif"/></center></font>'; 
}else{ 
    $message = '<font color="RED"><center>Database connection failed!<br />Please try again and/or check your connection info!<br />Error Given:<br />'.$dberror.'</center></font>'; 
} 

//database_pdo.php\\ 
class Database 
    { 
     var $db; 
     public function __construct($host, $user, $pass, $db) { 
      global $dberror; 
      try { 
       $this->db = new PDO("mysql:dbname=".$db.";host=".$host.";", $user, $pass);   
      } catch(PDOEXCEPTION $e) { 
       $dberror = 'An error has occurred! [Code: '.$e->getCode().']!<br/>More info: ['.$e->getMessage().']!'; 
      } 
     } 
     public function runQuery($query) { 
      global $dberror; 
      try{ 
       return $this->db->query($query); 
      } catch(PDOEXCEPTION $e) { 
       $dberror = 'An error has occurred! [Code: '.$e->getCode().']!<br/>More info: ['.$e->getMessage().']!'; 
      }   
     } 
    } 
?> 
FIXD