I have this:
class Database{
private $name = '';
private $user = '';
private $password = '';
private $host = 'localhost';
private $prefix = '';
private $connection_handle = null;
private function Connect(){
$this->connection_handle = mysql_connect($this->host, $this->user, $this->password);
if( !$this->connection_handle ){
die( 'Could not connect: ' . mysql_error() );
}else{
mysql_select_db( $this->name, $this->connection_handle );
}
}
private function Close(){
mysql_close( $this->connection_handle );
}
public static function Query( $query ){
$this->Connect();
$result = mysql_query( $query, $this->connection_handle );
if( !$result ){
die( 'Error: ' . mysql_error() );
}else{
$DatabaseQuery = new DatabaseQuery();
$DatabaseQuery->result = $result;
$DatabaseQuery->mysql_num_rows = mysql_num_rows($result);
return $DatabaseQuery;
}
$this->Close();
}
}
I have made Query static because it will be called like "Database::Query" The other functions need not be accessed by any other class so I made them private...
I get this error when accessing the Connect function from the Query function.
"Using $this when not in object context"
Having thought private makes those variables private to this class but available to all methods within it, I am having issues with getting scope of the functions...
I can do self::Connect() but I don't really understand it enough to warrant the use of it...
Can you explain the difference between self:: and %this->
I also cannot get access to my private variables and they really never need to be accessed outside of this class, once again I made them private because of this... and yet $this->connection_handle has no scope to the variable.
Do I really need to make them all public? there must be a way to make them available for this class only and extensions of it?
----------------------EDIT:///
I have ended up making all my variables private static. Is this an acceptable way of doing this?
----------------------EDIT:///
I have now got this code:
class Database{
private static $name = '';
private static $user = '';
private static $password = '';
private static $host = 'localhost';
private static $prefix = '';
private static $connection_handle = null;
private static function Connect(){
self::$connection_handle = mysql_connect(self::$host, self::$user, self::$password);
if( !self::$connection_handle ){
die( 'Could not connect: ' . mysql_error() );
}else{
mysql_select_db( self::$name, self::$connection_handle );
}
}
private static function Close(){
mysql_close( self::$connection_handle );
}
public static function Query( $query ){
self::Connect();
$result = mysql_query( $query, self::$connection_handle );
if( !$result ){
die( 'Error: ' . mysql_error() );
}else{
$DatabaseQuery = new DatabaseQuery();
$DatabaseQuery->$result = $result;
$DatabaseQuery->$mysql_num_rows = mysql_num_rows($result);
return &$DatabaseQuery;
}
self::Close();
}
}
But returning the pointer reference is not working? I'm actually not so up to date with PHP pointers, anyone shed some light on what I'm doing wrong here?
It's because i'm declaring the new instance of the DatabaseQuery class within the function isn't it. HELPPP :)
EDIT::///////
I have finished my class and it looks like this:
class DatabaseQuery{
public $result;
public $mysql_num_rows;
}
class Database{
private static $name = '';
private static $user = '';
private static $password = '';
private static $host = 'localhost';
private static $prefix = '';
private static $connection_handle = null;
protected function Connect(){
self::$connection_handle = mysql_connect(self::$host, self::$user, self::$password);
if( !self::$connection_handle ){
die( 'Could not connect: ' . mysql_error() );
}else{
mysql_select_db( self::$name, self::$connection_handle );
}
}
protected function Close(){
mysql_close( self::$connection_handle );
}
public static function FetchQueries( &$queries ){
$db_query = array();
self::Connect();
foreach( $queries as $key => $query ){
$result = mysql_query( $query, self::$connection_handle );
if( !$result ){
die( 'Error: ' . mysql_error() );
}else{
$DatabaseQuery = new DatabaseQuery();
$DatabaseQuery->result = $result;
$DatabaseQuery->mysql_num_rows = mysql_num_rows($result);
$db_query[ $key ] = $DatabaseQuery;
}
}
self::Close();
return $db_query;
}
}
Now you can call it in an Model View Control (MVC) way because you are defining all data at the top (or in a separate file)
$queries = array(
"bottoms" => "SELECT * FROM bottoms",
"users" => "SELECT * FROM users"
);
$dbr = Database::FetchQueries( $queries );
//display some users
while( $row = mysql_fetch_object( $dbr["apps"]->result ) ){
echo $row->title;
}
//display some bottoms
while( $row = mysql_fetch_object( $dbr["bottoms"]->result ) ){
echo $row->title;
}
Can you tell me what you think of this MVC method?