Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - PDO 'config' file equivalent

Tags:

php

pdo

This may be a question that has been answered before - if so please just leave a comment below and I'll remove this one.

I have been learning classes in PHP and at the same time making the jump to PDO.

One concept I cant seem to find is how to acomplish the equivalent to this with classes:

config.php

<?php

$host = 'localhost';
$user = 'user';
$pass = 'pass';

$con = mysql_connect($host, $user, $pass) or die("MySQL Error");
mysql_select_db("account_db", $con);

?>

another.php

<?php

require_once('config.php');

$selectStatement = "SELET foo FROM bar";
$selectQuery = mysql_query($selectStatement, $con);

?>

I haven't quite figured out how I would create a config file/class for a PDO connection and then use it in another class, i.e. Users as below:

<?php

class Users
{
    private $_userId;

    function setUserId($username)
    {
        // Use a predefined database handle to connect to a database to get the users ID - I assume using a preconfigured $dbh handle via an include or extend?
        $sth = $dbh->prepare("SELECT id FROM users WHERE username = :username");
        $sth->bindParam(':username', $username);
        ...
    }
}

?>

Thanks all :)

like image 382
lethalMango Avatar asked Mar 15 '11 16:03

lethalMango


People also ask

Which is better MySQLi or PDO?

Both MySQLi and PDO have their advantages: PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases. So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries.

Is PDO an ORM?

PDO and ORM are two entirely different things. PDO is a specific implementation of a Database Access Abstraction Layer, it enables you to connect, run SQL and retrieve results from the database with an API that is consistent across different database backends (e.g. MySQL, PostgreSQL, MS SQL, etc.)

What is dsn in PHP?

dsn. The Data Source Name, or DSN, contains the information required to connect to the database. In general, a DSN consists of the PDO driver name, followed by a colon, followed by the PDO driver-specific connection syntax. Further information is available from the PDO driver-specific documentation.

What is PDO :: Mysql_attr_init_command?

PDO::MYSQL_ATTR_INIT_COMMAND (string) Command to execute when connecting to the MySQL server. Will automatically be re-executed when reconnecting.


1 Answers

In my projects, I prefer using a class with a static member which holds the PDO object.

<?php
class DB
{
   private static $instance = null;
   public static function get()
   {
       if(self::$instance == null)
       {
           try
           {
               self::$instance = new PDO('mysql:host=localhost;dbname=name', 'user', 'abc123');
           } 
           catch(PDOException $e)
           {
               // Handle this properly
               throw $e;
           }
       }
       return self::$instance;
   }
}

The I can access it like so:

<?php
require 'DB.php';
class Users
{
    private $_userId;

    function setUserId($username)
    {
        // Using DB::get() to get the PDO object
        $sth = DB::get()->prepare("SELECT id FROM users WHERE username = :username");
        $sth->bindParam(':username', $username);
        ...
    }
}
like image 119
Tim Cooper Avatar answered Sep 29 '22 00:09

Tim Cooper