使用php pdo无法验证MySQL数据库

时间:2014-04-30 02:24:24

标签: php mysql 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().']!';
                    }              
            }
    }
?>

任何人都可以帮助我它会被贬低! 这是一个非常烦人的错误,所以必须尽快修复它 我在聊天中问了这个问题,但是没有人知道这可能会对我有所帮助

1 个答案:

答案 0 :(得分: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().']!';
            }        
        }
    }
?>