Connecting and Disconnecting Writing an Object-Oriented MySQL Interface for PHP

The class line nam es t he class, Cookbook_DB_Access , and t he extends clause indicat es t hat it s based on t he MySQL_Access class. Ext ending a class t his way is called subclassing t he base class, or creat ing a derived class from t he base class. The new class definit ion is alm ost t rivial, cont aining only variable assignm ent s for connect ion param et ers. These override t he em pt y values t hat are supplied by t he base class. The effect is t hat when you creat e an inst ance of t he Cookbook_DB_Access class, you get an obj ect t hat s j ust like a MySQL_Access obj ect , except t hat t he connect ion param et ers are set aut om at ically for connect ing t o t he cookbook dat abase. Now you can see m ore clearly why we left t he connect ion param et ers in t he MySQL_Access class em pt y rat her t han set t ing t hem for accessing t he cookbook dat abase. By leaving t hem blank, we creat e a m ore generic class t hat can be ext ended for any num ber of dat abases by creat ing different derived classes. Cookbook_DB_Access is one such class. I f youre w rit ing a set of script s t hat use a different dat abase, derive anot her ext ended class t hat supplies appropriat e connect ion param et ers for t hat dat abase. Then have t he script s use t he second ext ended class rat her t han Cookbook_DB_Access.php. I ncident ally, t he reason t hat Cookbook_DB_Access.php includes MySQL_Access.php is so t hat you dont need t o. When your script s include Cookbook_DB_Access.php, t hey get MySQL_Access.php for free. The include_once st at em ent is used rat her t han include t o prevent duplicat e-definit ion problem s from occurring if your script s happen t o include MySQL_Access.php anyway.

2.10.5 Connecting and Disconnecting

Now we need t o writ e t he m et hods of t he base class, MySQL_Access , t hat int eract wit h MySQL. These go in t he MySQL_Access.php source file. First , we need a connect m et hod t hat set s up a connect ion t o t he MySQL server: function connect { this-errno = 0; clear the error variables this-errstr = ; if this-conn_id == 0 connect if not already connected { this-conn_id = mysql_connect this-host_name, this-user_name, this-password; use mysql_errno mysql_error if they work for connection errors; use php_errormsg otherwise if this-conn_id { mysql_errno returns nonzero if its functional for connection errors if mysql_errno { this-errno = mysql_errno ; this-errstr = mysql_error ; } else { this-errno = -1; use alternate values this-errstr = php_errormsg; } this-error Cannot connect to server; return FALSE; } select database if one has been specified if isset this-db_name this-db_name = { if mysql_select_db this-db_name, this-conn_id { this-errno = mysql_errno ; this-errstr = mysql_error ; this-error Cannot select database; return FALSE; } } } return this-conn_id; } The connect m et hod checks for an exist ing connect ion and at t em pt s t o est ablish one only if it hasnt already done so. connect does t his so ot her class m et hods t hat require a connect ion can call t his m et hod t o m ake sure t here is one. Specifically, we can writ e t he query- issuing m et hod t o call connect before sending a query. That way, all a script has t o do is creat e a class obj ect and st art issuing queries; t he class m et hods aut om at ically t ake care of m aking t he connect ion for us. By writ ing t he class t his way, it becom es unnecessary for script s t hat use t he class ever t o est ablish a connect ion explicit ly. For a successful connect ion at t em pt , or if a connect ion is already in place, connect ret urns t he connect ion ident ifier a non- FALSE value . I f an error occurs, connect calls error and one of t wo t hings can happen: • I f halt_on_error is set , error print s a m essage and t erm inat es t he script . • Ot herwise, error does not hing and ret urns t o connect , which ret urns FALSE . Not e t hat if a connect ion failure occurs, connect t ries t o use mysql_errno and mysql_error if t hey are t he versions provided in PHP 4.0.6 and up t hat ret urn usable inform at ion for mysql_connect errors see Recipe 2.3 . Ot herwise, it set s errno t o - 1 and errstr t o php_errormsg . Ther e is also a disconnect m et hod corresponding t o connect in case you want t o disconnect explicit ly. Ot herwise, PHP closes t he connect ion for you when your script exit s. The m et hod calls mysql_close if a connect ion is open: function disconnect { if this-conn_id = 0 theres a connection open; close it { mysql_close this-conn_id; this-conn_id = 0; } return TRUE; }

2.10.6 Error Handling