SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DB Result Object Question?

    I'm wondering what the advantages of having you database results returned as in an object?

    Whats better to use:

    PHP Code:
    <?php
    class MySQLDriver {
        var 
    $link;
        
        function 
    __construct($host$user$password$name$pconnect FALSE$prefix NULL) {
            if (!
    $pconnect) {
                
    $this->link mysql_connect($host$user$password);
            } else {
                
    $this->link mysql_pconnect($host$user$password);
            }
            
            if (!
    $this->link) {
                  exit(
    'Error: Could not make a database connection using ' $username '@' $server);
            }

            if (!
    mysql_select_db($name$this->link)) {
                  exit(
    'Error: Could not connect to database ' $name);
            }
            
            
    $this->prefix $prefix;
          }        
        
          function 
    query($sql$data = array()) {
            if (!
    $data) {
                
    $result mysql_query($sql$this->link);
            } else { 
                
    $result vsprintf(str_replace(array_keys($data), array_values($data), $sql), array_map('mysql_real_escape_string'$data));
            }
             
            if (
    $result) {
                  return new 
    MySQLResult($result);
            } else {
                exit(
    'Error: ' mysql_error() . '<br />Error No: ' mysql_errno() . '<br />' $sql);
            }
          }

        function 
    escape($value) {
            return 
    mysql_real_escape_string($value$this->link);
        }
            
          function 
    countAffected() {
            return 
    mysql_affected_rows($this->link);
          }

          function 
    getLastId() {
            return 
    mysql_insert_id($this->link);
          }
        
        function 
    __destruct() {
            
    mysql_close($this->link);
        }
    }
    ?>

    <?php
    class MySQLResult {
        var 
    $result;
        
        function 
    __construct(&$result) {
            
    $this->result =& $result
          }
        
        function 
    getRow($key) {
            
    $result mysql_fetch_array($this->resultMYSQL_ASSOC);
            
            
    mysql_free_result($this->result);
            
            return 
    $result;
        }
        
        function 
    getRows() {
            
    $data = array();
            
            while (
    $result mysql_fetch_array($this->resultMYSQL_ASSOC)) {
                
    $data[] = $result;
            }
            
            
    mysql_free_result($this->result);

            return 
    $data;
        }
        
        function 
    countRows() {
            return 
    mysql_num_rows($this->result);
        }
        
        function 
    __destruct() {
            
    mysql_free_result($this->result);
        }
    }
    ?>
    or

    PHP Code:
    <?php
    class MySQLDriver {
        var 
    $link;
        var 
    $result;
        
        function 
    __construct($host$user$password$name$pconnect FALSE$prefix NULL) {
            if (!
    $pconnect) {
                
    $this->link mysql_connect($host$user$password);
            } else {
                
    $this->link mysql_pconnect($host$user$password);
            }
            
            if (!
    $this->link) {
                  exit(
    'Error: Could not make a database connection using ' $username '@' $server);
            }

            if (!
    mysql_select_db($name$this->link)) {
                  exit(
    'Error: Could not connect to database ' $name);
            }
          }        
        
          function 
    query($sql$data = array()) {
            if (!
    $data) {
                
    $this->result mysql_query($sql$this->link);
            } else { 
                
    $this->result vsprintf(str_replace(array_keys($data), array_values($data), $sql), array_map('mysql_real_escape_string'$data));
            }
             
            if (!
    $this->result) {
                exit(
    'Error: ' mysql_error() . '<br />Error No: ' mysql_errno() . '<br />' $sql);
            }
          }

        function 
    escape($value) {
            return 
    mysql_real_escape_string($value$this->link);
        }
            
          function 
    countAffected() {
            return 
    mysql_affected_rows($this->link);
          }

          function 
    getLastId() {
            return 
    mysql_insert_id($this->link);
          }
        

            
        function 
    getRow($key) {
            
    $result mysql_fetch_array($this->resultMYSQL_ASSOC);
            
            
    mysql_free_result($this->result);
            
            return 
    $result;
        }
        
        function 
    getRows() {
            
    $data = array();
            
            while (
    $result mysql_fetch_array($this->resultMYSQL_ASSOC)) {
                
    $data[] = $result;
            }
            
            
    mysql_free_result($this->result);

            return 
    $data;
        }
        
        function 
    countRows() {
            return 
    mysql_num_rows($this->result);
        }

        function 
    __destruct() {
            
    mysql_close($this->link);
        }
    }
    ?>

  2. #2
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Your just separating concerns. In, the latter case your class is doing too much.
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  3. #3
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As I wrote in your previous thread, if you convert results into arrays, you need to traverse this raw array for the second time to get formatted output. That's why I prefer something like

    PHP Code:
     echo Formatter::getTable(MySQLDriver::getRows($query)); 
    where getRows returns result.


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
  •