...

View Full Version : Resolved run query in different class



sitNsmile
03-29-2010, 04:02 AM
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)


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

$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

MattF
03-29-2010, 04:17 AM
Either pass the db var in the function parameters:



public function queryGames($type,$limit, $dbBroker){


or declare it global inside the function:



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

Fou-Lu
03-29-2010, 06:32 PM
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:


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.

sitNsmile
03-29-2010, 06:52 PM
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) ?

Fou-Lu
03-29-2010, 08:02 PM
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.

Dormilich
03-29-2010, 09:50 PM
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.

Fou-Lu
03-29-2010, 09:59 PM
>.<
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.

sitNsmile
03-29-2010, 11:22 PM
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)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum