Page 1 of 1

How does this MySQL class look?

Posted: Tue Nov 08, 2005 1:05 am
by Sequalit
How does this look? Is there a better way to check if i have an open connection or not?

Code: Select all

class db{
	var $username = "";
	var $password = "";
	var $database = "";
	var $connected = 0;
	
	function connect(){
		mysql_connect(localhost, $username, $password);
		@mysql_select_db($database) or die("The Database is currently unavaliable.");
		$this->$connected=1;
	}
	function disconnect(){
		mysql_close();
		$this->$connected=0;
        }
}
class core extends db{
	var $data = "";
	var $result;
	function query($string){
		$tempconn=0;
		if($this->$connected==0){
			$this->connect();
			$tempconn=1;
		}elseif($this->$connected==1){
			if(is_numeric($string)){
				$error = "Is numeric";
			}
			if(get_magic_quotes_gpc()){
				$string = stripslashes($string);
			}
			$string = "'".mysql_real_escape_string(
							html_entity_decode(
							htmlspecialchars(
								$string),ENT_NOQUOTES,"ISO-8859-1"))
						."'";
			$this->$result = mysql_query($string);
			if(!$this->$result){
				echo "An error has accured: <br />";
				echo mysql_error();
			}
			$this->$data=$result;
		}
		if($tempconn == 1){
			$this->disconnect();
			$tempconn = 0;
		}
	}
	function Row(){
		$this->$data=mysql_fetch_row($this->$result);
	}
	function Array(){
		$this->$data=mysql_fetch_array($this->$result);
	}
	function getData(){
		return $this->$data;
	}
	function getResult(){
		return $this->$result;
	}
	function free(){
		$this->mysql_free_result($this->$result);
	}
}
class member extends core{
}

Posted: Tue Nov 08, 2005 1:22 am
by feyd
quote localhost.

Your class properties for the data, results, and so forth aren't being stored correctly:

Code: Select all

$this->data
is the correct way.

$result is not the same as $this->result or $this->$result. They cannot be arbitrarily switched amongs the others..

Posted: Tue Nov 08, 2005 1:27 am
by Sequalit
so to fix this problem i would do

Code: Select all

$this->data = $this->result
Here is my updated code:

Code: Select all

class core extends db{
	var $data = "";
	var $result;
	var tempconn = 0;
	function query($string){
		if($this->connected==0){
			$this->connect();
			$this->tempconn=1;
		}elseif($this->connected==1){
			if(is_numeric($string)){
				$error = "Is numeric";
			}
			if(get_magic_quotes_gpc()){
				$string = stripslashes($string);
			}
			$string = "'".mysql_real_escape_string(
							html_entity_decode(
							htmlspecialchars(
								$string),ENT_NOQUOTES,"ISO-8859-1"))
						."'";
			$this->result = mysql_query($string);
			if(!$this->result){
				echo "An error has accured: <br />";
				echo mysql_error();
			}
			$this->data=$this->result;
		}
		if($this->tempconn == 1){
			$this->disconnect();
			$this->tempconn = 0;
		}
	}
	function Row(){
		if($this->connected==0){
			$this->connect();
			$this->tempconn=1;
		}elseif($this->connected==1){
			$this->data=mysql_fetch_row($this->result);
		}
		if($this->tempconn == 1){
			$this->disconnect();
			$this->tempconn = 0;
		}
	}
	function Array(){
		if($this->connected==0){
			$this->connect();
			$this->tempconn=1;
		}elseif($this->connected==1){
			$this->data=mysql_fetch_array($this->result);
		}
		if($this->tempconn == 1){
			$this->disconnect();
			$this->tempconn = 0;
		}
	}
	function getData(){
		return $this->data;
	}
	function getResult(){
		return $this->result;
	}
	function free(){
		if($this->connected==0){
			$this->connect();
			$this->tempconn=1;
		}elseif($this->connected==1){
			$this->mysql_free_result($this->result);
		}
		if($this->tempconn == 1){
			$this->disconnect();
			$this->tempconn = 0;
		}
	}
}

Posted: Tue Nov 08, 2005 9:56 am
by pickle
You're connecting and disconnecting every time a query is done. That could cause a lot of overhead if you end up using this class for lots of queries. I'd suggest connecting once when the object is created. That way, only one connection needs to be made per page. I wouldn't bother disconnecting after each query either - you may need the connection later - you could put the disconnection in your 'free()' function.

Same with 'Row()' - don't worry about disconnecting.

Posted: Tue Nov 08, 2005 11:45 pm
by Sequalit
alright, sounds good... am i doing the OOP correctly?

Posted: Wed Nov 09, 2005 10:00 am
by pickle
I'm by no means an OOP guru, but technically speaking, your approach seems fine.

FYI: 'accured' should 'occured'

Posted: Wed Nov 09, 2005 5:45 pm
by Sequalit
pickle wrote: FYI: 'accured' should 'occured'
Lol... yeah I have a hard time spelling sometimes, especially when its really early in the morning and im living off the caffine i get from DrPepper's and GreenTea lol.

awsome, its good to know im doing it correctly, I have experience with Java so OOP is not new to me and i love it, but its a bit confusing in PHP... tehehe.

Posted: Wed Nov 09, 2005 8:19 pm
by Jenk
If you are wishing to pass the object between page requests, make use of the __wakeup() and __sleep() functions

Posted: Wed Nov 09, 2005 10:48 pm
by Sequalit
okay those kind of confuse me... if i understand correctly

__sleep() puts your mysql connection on hold
__wake() puts your mysql connection on active duty

????

What exactly is serialize and unserialize for as well... from what i get they turn a varialbe into somethin like a session variable?

Posted: Thu Nov 10, 2005 12:10 am
by Sequalit
What about changing my connected variable to a global... so instead of doing this

Code: Select all

$this->connected=1
do this implimentation, that way i can have several classes extending from Core/Db

Code: Select all

class db{
     global $connected;
     function connect(){
          //blah
          $GLOBALS['connected'] = 1;
     }
}
That way in my index file, I can have a member class and a display news page

each would be extensions of Core

Code: Select all

$user = new member();
$page = new blogpage();

// all I would have to do is this right?
$user->connect();
//and I could do this
$page->query();
Also, what about making my actual object inside of GLOBALS, that way i can use it in each page?

Posted: Thu Nov 10, 2005 3:48 am
by Jenk
No. Globals will not transfer from page to page, it is only used for when you want to access a variable out of scope, not on different pages.

serialize() transforms your object into a string that can be stored in a session variable, database, blah blah blah

unserialize then restores the stored string to it's former glory, providing the class definitition is included before you try to unserialize() the object.

Try it.