First Generic Class
View Single Post
11-30-2007, 11:49 AM
Join Date: Nov 2007
I'd be happy to share what I am doing!
I create a CLASS that will act as a generic database interface
public class myDB()
NOTE: I'm pretty sure DB() is defined in the Zend framework and should be considered reserved ...
The class contains a number of private attributes:
private $_connection = null;
private $_rows_affected = 0;
private $_last_inserted_id = 0;
private $_results = null;
private $_error_message = null;
private $_error_number = null;
I use the __construct() method to set the connection string (I pull login and server variables from a separate configuration class e.g. myConfig::dbUsername, etc.) and I open the connection.
The __construct() method contains error handling that will SET the $_error_number and $_error_message attributes in the event of a failure.
I then have a public method called Query( $sql ) that allows me to pass SQL statements directly.
The Query() CLASS returns a bool (True or False) if the query was successful, but sets the above mentioned private attributes.
NOTE: Like __construct(), this method also contains error handling logic.
Finally, I have a series of Accessors that allow me to GET the values from my private attributes:
public function GetResults()
So to use this you do something like this:
$sql = "SELECT * FROM mytable WHERE somevalue = 1";
$db = new myDB();
if ( $db->Query( $sql ) )
doSomething( $db->GetResults() );
I hope this helps!
View Public Profile
Send a private message to bmicallef
Visit bmicallef's homepage!
Find More Posts by bmicallef