SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Still having trouble picking up on OP

    Well, I'm not even going to bother to post my attempt as its a joke, but what I am trying to accomplish is an SQL query class. I'm trying to figure out how to have the class open the connection using variables set in a 'global' php page (if possible) and then execute an array of sql queries (so that it doesn't have to open and close the connection more than once) and then finally close it. Problem is I don't know how to take that array and automatically name each query with a variable so it can be called later (eg. it automatically assigns results to $result1, $result2 etc.)

    Thanks for the help. I'm having trouble picking up on OP for some reason, can't get away from procedural

  2. #2
    Non-Member
    Join Date
    Oct 2008
    Location
    Banned
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you need a walkthrough or are you asking how to do it with an OP mindset? Just remember classes contain methods, and methods are basically just functions. Put each task in a class then reference it that way you wont ever have to go back and touch your code that you finished if there is a new spec on the program.

  3. #3
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Throw the variables in the constructor for the login info, or you can do something like read an ini file and pick up those values in an array. Then in your constructor setup a reference to the connection as a static property and then reference it when you do a loop through your array of sql queries, so only one connection is used.
    Cross browser css bugs

    Dan Schulz you will be missed

  4. #4
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    writing as I go along, so may not be exactly right, but something like this?

    Code PHP:
    class SQLConn {
    	private $conn = null;
    	public $result = array();
     
        public function __construct($host, $user, $pass, $db_name) {
        	$this->conn = mysql_connect($host, $user, $pass);
        	mysql_select_db($db_name);
    	}
     
    	public function query($qry_array) {
    		foreach ($qry_array as $query) {
    			$this->result[] = mysql_query($query, $this->conn);
    		}
     
    		return $this->result;
    	}
    }
     
    $sql = new SQLConn('host', 'user', 'pass', 'mydbname');
    $queries = array(
    	'SELECT * FROM table1';
    	'SELECT username FROM usertable WHERE userid = 1234';
    );
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,184
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    I'm having some trouble understanding your intentions. Would you like to pass the queries in as a array to the constructor and have them immediately executed or would like to be able to add an array of queries one by one after the object has been created.

    The first thing I would recommend is handling your connection separate from this manager type class. Your could then pass in the connection and rely on composition. Then on creation run all the queries and store the result in memory inside a private or protected property. Then use a method such as getResultSet($index) to pass in the index based on the index from the passed in queries array.

    Code:
    $db = new Connection($user,$pwd,$database,$host);
    $queries = array(
      'SELECT * from blah'
      ,'SELECT happy from whatever WHERE la=x'
    );
    $manager = new Manager($db,$queries);
    echo '<pre>',print_r($manager->getResultSet(1)),'</pre>';
    Code:
    class Manager {
    
      protected $_db;
      protected $_results = array();
    
      public function __construct(PDO &$pDb,$queries) {
        $this->_db =& $pDb;
        $this->_init($queries);
      }
    
      protected function _init($pQueries) {
        foreach($pQueries as $key=>$index) {
          /* execute query */
          /* store result */
          $this->_results[] = $result; /* result would be the result array from a single query */
        }
      }
    
      public function getResultSet($pIndex) {
        return $this->_results[$pIndex];
      }
      
      /* check if index exists in result array */
      public function hasResult($pIndex) {
        return array_key_exists($pIndex,$this->_results)?true:false;
      }
    }
    You may also want to consider making it into a iterator with a cursor and just setting the cursor then returning the array index that matches its current state.

  6. #6
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by triexa View Post
    writing as I go along, so may not be exactly right, but something like this?

    Code PHP:
    class SQLConn {
    	private $conn = null;
    	public $result = array();
     
        public function __construct($host, $user, $pass, $db_name) {
        	$this->conn = mysql_connect($host, $user, $pass);
        	mysql_select_db($db_name);
    	}
     
    	public function query($qry_array) {
    		foreach ($qry_array as $query) {
    			$this->result[] = mysql_query($query, $this->conn);
    		}
     
    		return $this->result;
    	}
    }
     
    $sql = new SQLConn('host', 'user', 'pass', 'mydbname');
    $queries = array(
    	'SELECT * FROM table1';
    	'SELECT username FROM usertable WHERE userid = 1234';
    );
    I'm having trouble printing the results from this for some reason. Is this tested?

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,184
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    That is because you need to loop through the result.

  8. #8
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did that

  9. #9
    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)
    It's not how I'd do it personally, but here's one with a little error logging...
    PHP Code:
    <?php
    class SQLConn {
        private 
    $conn null;
        private 
    $errors = array();
        public 
    $result = array();
       
        public function 
    __construct($host$user$pass$db_name) {
            
    $this->conn mysql_connect($host$user$pass);
            if(
    mysql_errno() > 0){
                
    $this->errors[] = mysql_error();
            }
            
    mysql_select_db($db_name);
            if(
    mysql_errno() > 0){
                
    $this->errors[] = mysql_error();
            }
        }
       
        public function 
    query($qry_array) {
            foreach (
    $qry_array as $query) {
                
    $this->result[] = mysql_query($query$this->conn);
                if(
    mysql_errno() > 0){
                    
    $this->errors[] = sprintf('Error executing "%s".',$query);
                }
            }
            return 
    $this->result;
        }
        
        public function 
    getErrors()
        {
            return 
    $this->errors;
        }

    ?>
    @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.

  10. #10
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kyle: use this, what do you see?

    Code PHP:
    class SQLConn {
        private $conn = null;
        public $result = array();
     
        public function __construct($host, $user, $pass, $db_name) {
            $this->conn = mysql_connect($host, $user, $pass);
            mysql_select_db($db_name);
        }
     
        public function query($qry_array) {
            foreach ($qry_array as $query) {
                $this->result[] = mysql_query($query, $this->conn);
            }
     
            return $this->result;
        }
    }
     
    $sql = new SQLConn('host', 'user', 'pass', 'mydbname');
    $queries = array(
        'SELECT * FROM table1';
        'SELECT username FROM usertable WHERE userid = 1234';
    );
    $results = $sql->query($queries);
    echo '<pre>';
    var_dump($results);
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  11. #11
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    array(2) {
    [0]=>
    resource(4) of type (mysql result)
    [1]=>
    resource(5) of type (mysql result)
    }

    When I did mysql_fetch_array on result[0] it gave a 'not a valid sql resource error', or is that wrong? I'm assuming it is after you had me do this.

  12. #12
    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)
    It looks fine, although it maybe wise to only add the new entry to the results array if the query was successful.

    That way, when you iterate the array, you know it only contains valid results.

    PHP Code:
    <?php
    class SQLConn {
        private 
    $conn null;
        private 
    $errors = array();
        public 
    $result = array();
       
        public function 
    __construct($host$user$pass$db_name) {
            
    $this->conn mysql_connect($host$user$pass);
            if(
    mysql_errno() > 0){
                
    $this->errors[] = mysql_error();
            }
            
    mysql_select_db($db_name);
            if(
    mysql_errno() > 0){
                
    $this->errors[] = mysql_error();
            }
        }
       
        public function 
    query($qry_array) {
            foreach (
    $qry_array as $query) {
                
    $rResult mysql_query($query$this->conn);
                if (
    is_resource($rResult)) {
                    
    $this->result[] = $rResult;
                }
                if(
    mysql_errno() > 0){
                    
    $this->errors[] = sprintf('Error executing "%s".',$query);
                }
            }
            return 
    $this->result;
        }
        
        public function 
    getErrors()
        {
            return 
    $this->errors;
        }
    }
    ?>
    @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.

  13. #13
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OOP is such a pain in the butt It's gonna take me awhile to get this.

  14. #14
    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)
    You'll be fine, just stick with it; you'll be glad you did.
    @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.

  15. #15
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya, you'll definitely be happy once you grasp it! Makes things so much simpler.
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  16. #16
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I know there's benefits, plan on sticking with it

    I am still a little confused on how to call or print the results from one query out of the array. I keep getting that it is an invalid sql resource.

  17. #17
    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)
    Use the class I provided, it will stop that error occurring.
    @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.

  18. #18
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kyle W View Post
    OOP is such a pain in the butt It's gonna take me awhile to get this.
    Pain in the butt? O.o your not supposed to be using your butt for OOP, that might be the problem XD... OOP is all about relationships -- how one piece of data relates to another. The difficulty comes defining those relation, but even then the difficulty is minimal.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  19. #19
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    Pain in the butt? O.o your not supposed to be using your butt for OOP, that might be the problem XD... OOP is all about relationships -- how one piece of data relates to another. The difficulty comes defining those relation, but even then the difficulty is minimal.
    HAHA! I just got a picture in my head from that commercial, things you cant do with your butt from Wii Fit I think? Archery was one of them, and that game where you whisper a phrase from person to person. Now oop... Oh man.

    Okay back on topic. SBUK, thanks again for your help, your class fixed up the problem. I really need to start building these myself soon


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
  •