I have a database class ... any improvements needed ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

I have a database class ... any improvements needed ??

Post by lazy_yogi »

Hi I just created (largely copied and modified actually) a class for database usage.

There are 2 goals I have for it :
1. sipmle interface
2. just a VERY SIMPLE and basic implementation. I don't need anything complicated

But if i missed anything obvious could you let me know
also .. any error checks that i missed would be helpful

cheers

Code: Select all

/****************************************************************
 * Description: PHP Class for MySQL Connection
 * 
 * EXAMPLES:
 * 
 * SELECT
 * include "class.mysql.php";
 * $mysql = New mysql('localhost','database','root','password');
 * if (! $mysql->connect()) echo $mysql->getError();
 * if (! $mysql->query("SELECT field FROM table")) echo $mysql->getError();
 * if ($mysql->num_rows() > 0) {
 * 	while ($mysql->movenext()) {
 * 		echo $mysql->getfield("field");
 *	}
 * }
 *  
 * INSERT
 * include "class.mysql.php";
 * $mysql = New mysql('localhost','database','root','password');
 * if (! $mysql->connect()) echo $mysql->getError();
 * if (! $mysql->query("INSERT INTO table (field) values ('value')")) echo $mysql->getError();
 * 
 * UPDATE
 * include "class.mysql.php";
 * $mysql = New mysql('localhost','database','root','password');
 * if (! $mysql->connect()) echo $mysql->getError();
 * if (! $mysql->query("UPDATE table SET field='newvalue' WHERE fieldID=1")) echo $mysql->getError();
 * 
 * DELETE
 * include "class.mysql.php";
 * $mysql = New mysql('localhost','database','root','password');
 * if (! $mysql->connect()) echo $mysql->getError();
 * if (! $mysql->query("DELETE FROM table WHERE fieldID=1")) echo $mysql->getError();
 * 
 *
 ***************************************************************/



class mysql {

	/* public: connection parameters */
	var $host;
	var $db;
	var $user;
	var $password;
	var $error;
	
	/* private: connection parameters */
	var $conn;
	var $result;
	var $record;

	function mysql ($host,$db,$user,$password) {
		$this->host = $host;
		$this->db = $db;
    		$this->user = $user;
		$this->password = $password;
		$this->error = "";
	}
		
	function connect () { 
		if (! $this->conn = mysql_pconnect($this->host,$this->user,$this->password)) {
			$this->error = "Connection to $server failed : ".mysql_error()."<br>\n";
			return false;
		}
         
		if (! mysql_select_db($this->db,$this->conn)) {
			$this->error = "Error:" . mysql_errno() . " : " . mysql_error() . "<br>\n";
			return false;
		}            	

		return true;			
	}

	function query($sql) {
		if (! $this->result = mysql_query($sql, $this->conn)) {
			$this->error = "Error:" . mysql_errno() . " : " . mysql_error() . "<br>\n";
			return false;
		}
		return true;
	}

	function num_rows() {
		return mysql_num_rows($this->result);
	}

  	function movenext(){
		$this->record = mysql_fetch_array($this->rstemp);
		return (is_array($this->record));
	}

	function getfield($field){
		return($this->record[$field]);
	}

	function getError(){
		return $this->error;
	}

	function close(){
		mysql_close($this->conn);
	}
}

oh and one more thing :

Is this the best way to safetly execute a query (uses mysql_escape_string):

Code: Select all

result = mysql_query(mysql_escape_string($sql), $this->conn)
cheers
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

Anyone ?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

What do you gain to make the overhead of the class worthwhile?

In the first place there's the class itself, then there's lots of functions which don't do much. If you could achieve the same results without a class - and without defining lots of functions - that would be more efficient.

That's a genuine question as much as criticism - I'm still working out when to use and not use classes.
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

hmm .. excellent point

... on looking at it, I use exactly the same amount of code.

thanx for the food for thought

Cheers
Post Reply