簡體   English   中英

無法使用php pdo驗證MySQL數據庫

[英]MySQL database cannot be verified using 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().']!';
                    }              
            }
    }
?>

有人可以幫助我嗎? 這是一個非常煩人的錯誤,因此我必須在聊天中盡快解決它,但沒人知道,所以這可能會對我有所幫助

嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM