Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Is anyone around, to give some information in regards to the difference between the two queries, pdo connection class and which is better and why?

What I have tried:

PDO Database Connection Class 1
class DB extends PDO {

    protected $db_name = "yog";
    protected $db_user = "root";
    protected $db_pass = "";
    protected $db_host = "localhost";

    public function __construct() {
        try {
            parent::__construct("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user, $this->db_pass);
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    } 


PDO Database Connection Class 2
class DB {

    private $dbHost     = "localhost";
    private $dbUsername = "root";
    private $dbPassword = "";
    private $dbName     = "yog";

    public function __construct(){
        if(!isset($this->db)){
            // Connect to the database
            try{
                $conn = new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName, $this->dbUsername, $this->dbPassword);
                $conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $this->db = $conn;
            }catch(PDOException $e){
                die("Failed to connect with MySQL: " . $e->getMessage());
            }
        }
    }   
Posted
Updated 9-Jan-19 15:30pm
v2
Comments
Mohibur Rashid 9-Jan-19 17:46pm    
I would suggest you not to use the second function. Since you are extending PDO, you should not write the connection function. It's look like in second function you are pointing to connect through this object, better is not to do this, rather use the first function and extend your other required functions
Member 14093672 9-Jan-19 19:39pm    
Hi Mohibur AS Salam O ALykum, please kindly give some explanation and why we cannot use it in simple terms please. Jazakallah Khair

1 solution

Walaikum Salam.

It seems, you have removed extension from the second class. Now Second class look ok.

Which one you will use depends on your approach.
But second class has a variable $db; which is not private. Can be modified out side of the class, and at the beginning of the class you are checking whether $this->db exists or not; in construction $this->db does not exists anyway;


PHP
<?php
class cc {
  public function __construct() {
    $this->a = 10;
    $this->b = 20;
  }
}

$x= new cc();
print_r($x);


Result:
cc Object
(
    [a] => 10
    [b] => 20
)

Which mean it works; but not a good practice. Will lead to unexpected disaster.

Your first class,
$db = new DB();
$res = $db->query("The Query");

This has great advantage. It will let you directly access any function of PDO Class without any restriction. This is useful, when working as team. You still can add functions to do additional job. And you can wrap existing function of PDO if you have to.

On the other hand, with second class, you will have to implement many functions to access individual function, this is going to be problematic in the long run.

So, I will still suggest you to use the first class.




SQL
-------------ADDITIONAL-------------

About the question you have asked related to set attribute:
class DB extends PDO {
...
...
}

$conn = new DB();
conn->setAttribute(PDO::CONSTANT, PDO::CONSTANT); <-- this will solve your attribute setting issue. Please, take your time to understand how class, extended class  works. 

// This should work too
$conn->setAttribute(DB::CONSTANT, DB::CONSTANT);



You should not open more than one connection in a single http request, you need to find a way to let the connection float around in the entire request and make sure that connection closed at the end of request.
 
Share this answer
 
v2
Comments
Member 14093672 10-Jan-19 10:00am    
Jazakallah Khair, I was in a dilemma and i was unsure about security impact on the application using the first approach until i heard from your timely and expert advise.

You are absolutely right, It is lot easier to use the PDO Data access using he first approach. I am still learning day to day.

include 'DB.php';
$con = new DB();

But the only issue is, how do i set

$con -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

and also re-use the connection if still opened.


Is there a way I can get in touch with you via email, if that's ok with you?

Once again million and billion thanks.
Mohibur Rashid 10-Jan-19 19:43pm    
I will answer your original question later. about contacting me, It's easier to ask here. Because, many people have good suggestion to offer. And for the sake of my own safety(in terms of spam mail) I will not post my email address at any forum. And you should refrain yourself from posting your email address
Mohibur Rashid 10-Jan-19 19:55pm    
I Have updated the answer.
Member 14093672 11-Jan-19 8:45am    
When i tried any of the above, I am getting an error undefined class constant.
Mohibur Rashid 13-Jan-19 4:05am    
What's the error?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900