Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts

    run query in different class

    Okay, im trying to access my "mysqli.php" class to run the query through another class in another file. soemthing like

    $dbBroker (is located in mysqli.php)
    queryGames (located in class.games.php)
    PHP Code:
    public function queryGames($type,$limit){
            
    $sql "SELECT * FROM games_info WHERE g_status = 1 ORDER by $type LIMIT $limit";
    $runSql $dbBroker->arcade->fetchAll($sql);

    return 
    $runSql;
     
        } 
    fetchAll (is the query in my mysqli class) but how do I access it from here. The index already declares the database and the myswli class, but in here doesn't understand where that variable is.

    and when I run this
    PHP Code:
    $gamesData->queryGames('g_dateadded','10'); 
    of course I get the Fatal error: Call to a member function fetchAll() on a non-object

    But any idea how to get the fetchAll() (query to work in this?)

    Thanks
    Last edited by sitNsmile; 03-29-2010 at 08:31 PM.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Either pass the db var in the function parameters:

    Code:
    public function queryGames($type,$limit, $dbBroker){
    or declare it global inside the function:

    Code:
    public function queryGames($type,$limit){
        global $dbBroker;

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Reverse you're logic behind handling of this. class.games.php seems to be controlling more than it should; you're database handler should take care of this. You can definitely chain these; however, do not write you're SQL in a class that uses the SQL, rather write the implementation inside of the driver for it instead. Most easily done by implementing a custom interface to guarentee function availability. This is something that kinda 'clicks' and then you can use it from that point on. We'll just use something simple here for demonstration:
    PHP Code:
    interface IDatabaseDriver
    {
        public function 
    select(array $aFields$sTable$sCondition null$sOrder null$sDirection 'ASC'$iLimitFrom = -1$iLimitTo = -1);
    }

    class 
    MySQLiDriver implements IDatabaseDriver
    {
        
    // We'll have a constructor responsible for establishing communication
        // You'll also want to of course secure this, this is just for an example, not for usage
        
    public function select(array $aFields$sTable$sCondition null$sOrder null$sDirection 'ASC'$iLimitFrom = -1$iLimitTo = -1)
        {
            
    $sQry sprintf('SELECT %s FROM %s'implode(', '$aFields$sTable);
            if (!empty(
    $sCondition))
            {
                
    $sQry .= ' WHERE ' $sCondition;
            }
            if (!empty(
    $sOrder))
            {
                
    $sQry .= ' ORDER BY ' $sOrder ' ' .  $sDirection;
            }
            if (
    $iLimitFrom 0)
            {
                
    $sQry .= ' LIMIT ' $iLimitFrom;
                if (
    $iLimitTo 0)
                {
                    
    $sQry .= ' ' $iLimitTo;
                }
            }

            return 
    $this->dbConn->query($sQry);
        }
    }

    class 
    Games
    {
        private 
    $idd;
        public function 
    __construct(IDatabaseDriver $idd)
        {
            
    $this->idd $idd;
        }

        public function 
    queryGames($type,$limit){
            return 
    $this->idd->select(array('*'), 'games_info''g_status = 1'$type$limit); 
        }  

    }


    // Usage
    $games = new Games(new MySQLiDriver('...'));
    $qry $games->queryGames('SomeGameTypeToOrderBy'5);
    // This $qry is a RESULTSET, but we do not want to allow our primary program to iterate it. 
    Advantages of this approach:
    The driver determines what it needs to do. With an Oracle, MSSQL and MySQL driver available, changing them is a matter of switching the driver type. Each class is designed to deal with their own custom features (such as the LIMIT commands), but we needn't rescript our current SQL to deal with it. Portability of code increase is 10 fold since we are no longer bound to a particular datasource. So long as its done properly, IDatabaseDriver could be a flatfile or XML interfaced version of the same code, but we needn't concern ourselves with that underlying detail.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    sitNsmile (03-29-2010)

  • #4
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Fou-Lu View Post
    Reverse you're logic behind handling of this.
    I like the idea you've come up with. I've been thinking of a good query engine I'd like to produce with the class, but with what you've explained, how does this have any connection to my already designed mysqli class (connection to the database) ?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    It doesn't; you shouldn't actually have a class named MySQLi since thats a PHP5+ class that can be enabled. The drivers themselves are custom, and will instead call their corresponding database type.
    What I'm trying to get at is hit it early. Sandbox all of you're SQL itself within individual drivers / functions / pages / whatever, simply so that when you make a storage change its a matter of switching the drivers instead of rewriting the SQL code itself.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,291
    Thanks
    13
    Thanked 345 Times in 341 Posts
    Quote Originally Posted by Fou-Lu View Post
    Advantages of this approach:
    The driver determines what it needs to do. With an Oracle, MSSQL and MySQL driver available, changing them is a matter of switching the driver type.
    if you want it to run on different databases, use PDO, that’s made exactly for that purpose.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    >.<
    Except I'm getting at that SQL itself is different between different databases engines. PDO can certainly be used in place of a targeted driver if desired, though will likely be slower.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    well sites im designing this engine for are more "arcade" style. higher end traffic, need to use less server resource, and have a fast efficient script. I'd love to develop it all in OO style, but finding that with high end traffic sites; its just too slow, so I have to some how do a little of both.

    need to plan the best options. The idea is to take the entire script that company is using which was built years ago all in Procedural. (horrible code)
    Last edited by sitNsmile; 03-30-2010 at 12:10 AM.


  •  

    Posting Permissions

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