Re: MySQL class. Thoughts?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jan 21, 2009 at 11:37:07AM -0600, Jay Moore wrote:

> This is a MySQL class I use and I wanted to get everyone's thoughts on
> how/if I can improve it.  This is for MySQL only.  I don't need to make
> it compatible with other databases.  I'm curious what you all think.
>
> Thanks,
> Jay
>
> Class:
> ------
> <?php
>
> // Standard MySQL class
> class do_mysql
> {
> 	// Constructor
> 	function __construct()
> 	{
> 		$this->do_mysql();
> 	}
>
> 	// Destructor
> 	function __destruct()
> 	{
> 		//$this->close();
> 	}
>
> 	function do_mysql()
> 	{
> 		$this->login = '';
> 		$this->pass = '';
>
> 		$this->link = @mysql_connect('localhost', $this->login,
> 		$this->pass)
> or die('Could not connect to the database.');
> 	} // End do_mysql
>
> 	// Functions
> 	function close()
> 	{
> 		if ($this->link)
> 		{
> 			mysql_close($this->link);
> 			unset($this->link);
> 		}
> 	} // End close
>
> 	function fetch_array()
> 	{
> 		return mysql_fetch_array($this->result);
> 	} // End fetch_array
>
> 	function last_id()
> 	{
> 		return mysql_insert_id($this->link);
> 	} // End last_id
>
> 	function num_rows()
> 	{
> 		return mysql_num_rows($this->result);
> 	} // End num_rows
>
> 	function process($database = '')
> 	{
> 		if (is_null($this->query))
> 		{
> 			die('Error:  Query string empty.  Cannot proceed.');
> 		}
>
> 		$this->db = @mysql_select_db($database, $this->link)
> 		or die("Database
> Error:	Couldn't select $database <br />" . mysql_error());
> 		$this->result = @mysql_query($this->query, $this->link) or
> die('Database Error:  Couldn\'t query. <br />' . mysql_error() . "<br
> /><br /> $this->query");
> 	} // End process
>
> 	function sanitize(&$ref)
> 	{
> 		$ref = mysql_real_escape_string($ref);
> 	} // End sanitize
>
> } // End do_mysql
>
> ?>
>
>
> Sample usage:
> $value = 'value';
> $sql = new do_mysql();
> $sql->sanitize($value);
> $sql->query = "SELECT * FROM `wherever` WHERE `field` = '$value'";
> $sql->process('dbname');
> $sql->close();
>
> if ($sql->num_rows())
> {
> 	while ($row = $sql->fetch_array())
> 	{
> 		do stuff;
> 	}
> }
>

A couple of thoughts. First precede all your mysql_* calls with the at
sign (@) to shut up the routines if they generate text. I had this
problem, and that was the answer.

Second, store your connection resource as a class variable, so you can
pass it around to the various routines. Actually, you're already doing
this, but I prefer to do so explicitly, as:

var $link;

at the top of the class.

I have a similar class for PostgreSQL. I also have routines like
"update", which allow you to pass a table name and an associative array
of field values. Same thing for an "insert" routine.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux