OOP Best Practices - Queries
Posted: Tue Jul 29, 2008 12:25 pm
I have class called generalClass which runs queries, connects to the db and so on. I have another class for a specific section of a website, in this case peopleClass. Right now peopleClass only has queries but may include functions later.
Is it a good or bad idea to keep my queries for this section in the class? How is the overall approach?
Is it a good or bad idea to keep my queries for this section in the class? How is the overall approach?
Code: Select all
##General Class
class generalClass{
protected $dbc;
protected $errorMsg="";
public function __construct(){
if (stristr($_SERVER['HTTP_HOST'], 'local')||(substr($_SERVER['HTTP_HOST'],0,7)=='192.168')){
$local=TRUE;
}else{
$local=FALSE;
}
if($local){
$this->dbc=mysqli_connect('localhost','root','','db') or die (mysql_error());
}else{
$this->dbc=mysqli_connect('remote','abc','abc','db') or die (mysql_error());
}
}
public function singleResult(){
$result=mysqli_query($this->dbc,$this->query) or die (mysql_error());
if($result){
$row=mysqli_fetch_assoc($result);
}else{
$this->errorMsg="Sorry, the result was empty.";
}
return $row;
}
}
### more functions...
Code: Select all
##People Class
require_once('./library/class/general.class.php');
class peopleClass extends generalClass{
protected $query;
public function peopleQuery(){
$this->title=($_GET['title']);
$this->query="SELECT people_fname, people_lname WHERE people_title='this->$title' ORDER BY people_lname ASC";
return $this->query;
}Code: Select all
##Sample Page
#People
require_once './library/class/people.class.php';
//Template
$template="default.template.php";
//Content
$people=new peopleClass();
$people->peopleQuery();
$item=$people->singleResult();
$tags['content'].=$item['people_fname'];