duancanjiu3754 2014-04-30 02:24
浏览 37
已采纳

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

well the title says enough I guess! my db connection always fails for the verification for some reason...

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

can anybody help me it would be apperciated! it's a really annoying error so it must be fixd as soon as possible I asked it in the chat but neither one knew so this might help me better

  • 写回答

1条回答 默认 最新

  • dopt85756 2014-04-30 02:25
    关注

    Try this

    <?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().']!';
                }        
            }
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题