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