Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用php pdo验证MySQL数据库_Php_Mysql_Pdo - Fatal编程技术网

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

无法使用php pdo验证MySQL数据库,php,mysql,pdo,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

好吧,我想标题说得够多了! 由于某种原因,我的数据库连接总是无法验证

<?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().']!';
                    }              
            }
    }
?>
试试这个

<?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().']!';
            }        
        }
    }
?>