SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Defining $this->variable

    Hey,

    The more variables I declare at the beginning, the slower the application becomes?

    I'm thinking of two ways to generically pull information from a database with fetch a Assoc and fetch as an Array.

    PHP Code:
    public function fetchAssoc()
    {
      
    $this->fetchAssoc mysql_fetch_assoc($this->query$this->link);
      if (!
    is_resource($this->fetchAssoc))
        Throw New 
    Exception(mysql_error());
      else
      {
        
    $array = array();
        while (
    $field $this->fetchAssoc)
        {
           
    $array[] = $field
        }
        return 
    $array;
      }

    Or would it be better if I just did something like this:

    PHP Code:
    public function fetchAssoc()
    {

      
    $array = array();
      while (
    $field mysql_fetch_assoc($this->query$this->link))
      {
         
    $array[] = $field
      }
      return 
    $array;


    What would be better for performance, and security? I know though that using the first method would actually benefit me in certain ways because I'm error handling it properly.

    What do you guys think?

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is how I do it and have seen many people do it, so I hope this helps.

    PHP Code:
    class Results {
        public 
    $results_array = array();
        
    ###
        
    public function result($type "object") {
            return (
    $type == "object") ? $this->result_object() : $this->result_array();
        }
        
    ###
        
    public function result_array() {
            if (
    count($this->results_array) > 0) {
                return 
    $this->results_array;
            }
            if (!
    $this->result_id || ($this->total_rows() == 0)) {
                return array();
            }
            
    $this->_data_seek(0);
            while (
    $row $this->_fetch_assoc()) {
                
    $this->results_array[] = $row;
            }
            return 
    $this->results_array;
        }
        
    ###
        
    public function _fetch_assoc() {
            return array();
        }
        
    ###
    }

    class 
    MySQLResuts extends Results {
        
    ###
        
    function _fetch_assoc() {
            return 
    mysql_fetch_assoc($this->result_id);
        }
        
    ###



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
  •