简体   繁体   中英

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. 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. 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:

$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 ")

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

Source : PHP.net : PDO connections

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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