SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Opinion sought on this code please

    I've been playing around with PDO and created this class.

    Do you think this is efficient code? Look at the way I've created two different methods and called the first in the second


    Code:
    class Database
    {
        
    
        public function connect_database()
        {
    
            # connect to the database
            try {
    
                $dsn = 'mysql:dbname=testing;host=localhost';
                $user = 'root';
                $password = '';
                $DBH = new PDO($dsn, $user, $password);
                // There are three different levels of PDO error message: http://php.net/manual/en/pdo.error-handling.php
                $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
                return $DBH;
    
            }
    
            catch (PDOException $e) {
    
                echo "There has been an error connecting to the database: ";
                echo br;
                echo $e->getMessage();
    
            }
    
        } // End function connect_database();
    
        public function select_data($sql)
        {
            // Call connect_database() method
            $DBH = $this->connect_database();
        
    
            try {
    
                $query = $DBH->query($sql);
    
                while ($result = $query->fetch(PDO::FETCH_ASSOC)) {
    
                    return $result;
    
                }
            }
    
            catch (PDOException $e) {
    
                echo "There has been an error with this select query: ";
                echo br;
                echo $e->getMessage();
    
            }
    
    
        } // End public function select data
        
    } // End class Database
    
    $db = new Database();
    
    $db->select_data("SELECT * FROM user");
    Let everyday be Christmas

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Try something a little more like this:-
    PHP Code:
    <?php
    class Database
    {
      protected
        
    $connection;

      public function 
    __construct($host$schema$username$password){
        try{
          
    $this->connection = new PDO(
            
    sprintf('mysql:dbname=%s;host=%s'$schema$host),
            
    $username,
            
    $password
          
    );
        }catch(
    Exception $e){
          throw new 
    Exception('Cannot connect to database');
        }
      }
      
      public function 
    execute($sql){
        
    $res $this->connection->query($sql);
      }
    }

    try{
      
    $db = new Database('127.0.0.1''database''user''pass');
    }catch(
    Exception $e){
      echo 
    'Sorry, we cannot connect to the database';
      exit;
    }
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, with PDO does the error of try / catch only work for the database connection or can it be used on sql queries?

    Code:
    try {
    .. sql query
    }
    
    catch(PDOException $e) {
    
    }
    Let everyday be Christmas


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •