简体   繁体   English

PHP PDO数据库连接类

[英]php pdo db connection class

<?php

    class DB_Handler{

        private $host;
        private $db_name;
        private $db_username;
        private $db_password;
        public $dbh;

        public function __construct( $db_host, $db_name, $db_username, $db_password ){

            $this->host = $db_host;
            $this->db_name = $db_name;
            $this->db_username = $db_username;
            $this->db_password = $db_password;
        }

        public function connect(){
            try{                
                $this->dbh = new PDO("mysql:host=$this->host; db_name =$this->db_name, $this->db_username, $this->db_password");

                $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }
            catch(PDOException $e){
                $error = "Error: ".$e->getMessage().'<br />';
                echo $error;
                return false;
            }
            return true;
        }   

    }

    $db_host = "localhost";
    $db_name = "db_handler_01";
    $db_username = "root";
    $db_password = "sd";

    $snowboard_db = new DB_Handler($db_host, $db_name, $db_username, $db_password);

    $result = $snowboard_db->connect();



    if($result){
        echo "DB Connected <br>";
        echo $snowboard_db;
    }
    else{
        echo "DB is not Connected <br />";      
    }

?>

If i give the db details right or wrong it is showing the DB Connected only. 如果我给数据库详细信息是对还是错,它仅显示数据库已连接。 Please let me know where i am doing mistake. 请让我知道我在哪里做错。

There are two possible problems here. 这里有两个可能的问题。

The first has already been mentioned, you need to send the username and password as the second and third argument of the PDO constructor. 第一个已经提到过,您需要发送用户名和密码作为PDO构造函数的第二个和第三个参数。 You are sending everything in the first parameter due to a - probably misplaced - quote. 由于引号-可能放错了位置,因此您正在发送第一个参数中的所有内容。

The second problem is that you tell PDO to throw exceptions after you have made (or tried to make) the connection. 第二个问题是您在建立(或尝试建立)连接告诉PDO引发异常。 For connection errors that is probably to late. 对于连接错误,可能要晚了。

If you want to catch connection errors as well, you can send a 4th parameter to the PDO constructor with the required options: 如果还想捕获连接错误,则可以使用必需的选项将第4个参数发送到PDO构造函数:

$opt = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$this->dbh = new PDO("mysql:host={$this->host}; db_name ={$this->db_name}", 
                     $this->db_username, $this->db_password, $opt);
$this->dbh = new PDO("mysql:host=$this->host; db_name =$this->db_name,
                      $this->db_username, $this->db_password");

Syntax is incorrect (u messed with final ") 语法不正确(u弄混了最终的“”)

Correct syntax is : 正确的语法是:

$this->dbh = new PDO("mysql:host=$this->host;db_name=$this->db_name",
                          $this->db_username, $this->db_password);

BTW u cannot echo your class, it'll return : 顺便说一句,你不能回应你的课程,它将返回:

Catchable fatal error: Object of class DB_Handler could not be converted to string in C:...\\index.php on line 49 可捕获的致命错误:无法在第49行的C:... \\ index.php中将类DB_Handler的对象转换为字符串

Source : PHP.net : PDO connections 来源: PHP.net:PDO连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM