Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to make a PDO mysql static connection class?

I'm quite new to PDO and also OOP with PHP in general so please be nice :) Basically I'm trying to make a connection object based on PDO so that I can have one connection that I call throughout my site.

I need some prepared statements that simply look up different results based on an ID I pass through using the same db object that I'm trying to create below.

How do I make and access the db class I set below and then use functions within it to extract the relevant info I need? Any examples would be great so I can get an idea of the best practices etc.

Many thanks in advance.

class db {

    private static $connection;

    private function __construct(){}
    private function __clone(){}

    private static function connect($db_server="localhost", $db_user="user", $db_pass="password") {
        if(!$this->connection){
            try{
                $this->connection = new PDO($db_server, $db_user, $db_pass);
            } catch (PDOException $e) {
                $this->connection = null;
                die($e->getMessage());
            }
        }
        return $this->connection;
    }

}

$dbh = new db::connect();

$stmt = $dbh->prepare("SELECT * FROM questions where id = ?");
if($stmt->execute(array($_REQUEST['testid']))) {
  while ($row = $stmt->fetch()) {
    print_r($row);
  }
}
like image 670
Tim Avatar asked Mar 15 '12 20:03

Tim


People also ask

What is the method to close the connection in Mysqlli and PDO?

With MySQLi, to close the connection you could do: $this->connection->close(); However with PDO it states you open the connection using: $this->connection = new PDO();

Which type of databases can PDO connect to?

MySQLi procedural and MySQLi object-oriented only support MySQL database but PDO is an advanced method along with MySQL which supports Postgres, SQLite, Oracle, and MS SQL Server.

What is the use of PDO class in PHP?

What Is PDO? PDO in PHP offers a data-access abstraction layer, which means you can issue queries and fetch data using the same functions regardless of which database you're using. PDO isn't a database abstraction; it doesn't rewrite SQL or imitates features that aren't accessible.


1 Answers

You could begin by not ever using Singleton pattern again. It (and static classes in general) is bad for all the same reasons why global variables in procedural programming are bad.

That said ... Instead of trying to enforce the uniqueness of the connection object, you should be just making sure that you using the same connection all over the place.

Here's an example of what i mean by that:

class Foo
{
    protected $connection = null;
    public function __construct( PDO $connection ) 
    {
        $this->connection = $connection;
    }
}

class Bar
{
    // all the same as in Foo
}

$connection = new PDO('sqlite::memory');

$foo = new Foo( $connection );
$bar = new Bar( $connection );

At this point both $foo and $bar objects will have access to the same PDO instance. If you have an object that needs access to database, then you just provide it with a connection in the constructor.

There are two videos you might want to watch (slides will contain Java code, but you should have no troble understanding it):

  • Global State and Singletons
  • Don't Look For Things!
like image 119
tereško Avatar answered Sep 30 '22 23:09

tereško