Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #1849
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: database access |
| Date | 2011-05-26 09:00 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <irlitn$lan$1@dont-email.me> (permalink) |
| References | <7ded58f7-d089-4a1e-ba94-38183b0ec851@z37g2000vbl.googlegroups.com> <irjspv$iap$1@dont-email.me> <irlfu3$3nk$1@dont-email.me> |
On 5/26/2011 8:09 AM, sheldonlg wrote:
> On 5/25/2011 5:37 PM, Jerry Stuckle wrote:
>> On 5/25/2011 1:17 PM, Jivanmukta wrote:
>>> I new relatively new to PHP. I need your opinion if the following set
>>> of database functions make sense:
>>>
>>> function openDatabase() {
>>> global $db;
>>> try {
>>> $db = new PDO('mysql:host=' . MYSQL_SERVER . ';dbname=' .
>>> MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD);
>>> $db->exec('SET NAMES latin2');
>>> $db->exec('SET CHARACTER SET latin2 COLLATE latin2_general_ci');
>>> $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
>>> register_shutdown_function('closeDatabase');
>>> } catch (PDOException $e) {
>>> return false;
>>> }
>>> return true;
>>> }
>>>
>>> function closeDatabase() {
>>> global $db, $inTransaction;
>>> if ($inTransaction) {
>>> $db->rollBack();
>>> $inTransaction = false;
>>> }
>>> if (!is_null($db)) {
>>> $db = null;
>>> }
>>> }
>>>
>>> function queryDatabase($sql) {
>>> global $db, $inTransaction;
>>> $result = array();
>>> try {
>>> $qry = $db->query($sql);
>>> if (!empty($qry)) {
>>> $result = $qry->fetchAll();
>>> }
>>> } catch (PDOException $e) {
>>> if ($inTransaction) {
>>> $db->rollBack();
>>> }
>>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>> }
>>> return $result;
>>> }
>>>
>>> function execDatabase($sql) {
>>> global $db, $inTransaction;
>>> $result = array();
>>> try {
>>> $result = $db->exec($sql);
>>> } catch (PDOException $e) {
>>> if ($inTransaction) {
>>> $db->rollBack();
>>> }
>>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>> }
>>> return $result;
>>> }
>>>
>>> function startTransaction() {
>>> global $db, $inTransaction;
>>> $db->beginTransaction();
>>> $inTransaction = true;
>>> }
>>>
>>> function commitTransaction() {
>>> global $db, $inTransaction;
>>> $inTransaction = false;
>>> if ($db->commit()) {
>>> return;
>>> }
>>> $db->rollBack();
>>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>> }
>>>
>>> function rollbackTransaction() {
>>> global $db, $inTransaction;
>>> $inTransaction = false;
>>> if ($db->rollBack()) {
>>> return;
>>> }
>>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>> }
>>>
>>> function lockTables($tables) {
>>> return execDatabase('LOCK TABLES ' . join(' WRITE, ', $tables) . '
>>> WRITE');
>>> }
>>>
>>> function unlockTables() {
>>> return execDatabase('UNLOCK TABLES');
>>> }
>>
>> Kind of, and maybe a bit overkill. But personally I'd put it all in a
>> Database class. The code would be more flexible and less prone to errors.
>>
>
> Further, I would not have globals. I would pass $db in via the
> constructor. Likewise, I would have $inTransaction be a class variable
> which gets set by the method startTransaction().
>
That was part of the class, Sheldon. In fact, I wouldn't even pass $db
into the constructor; it (along with $inTransaction, as you note) would
be class variables. That way the rest of the program is completely
independent of the database (and you can have more than one database
active concurrently if you want).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
database access Jivanmukta <jivanmukta@poczta.onet.pl> - 2011-05-25 10:17 -0700
Re: database access Jerry Stuckle <jstucklex@attglobal.net> - 2011-05-25 17:37 -0400
Re: database access sheldonlg <sheldonlg@thevillages.net> - 2011-05-26 08:09 -0400
Re: database access Jerry Stuckle <jstucklex@attglobal.net> - 2011-05-26 09:00 -0400
Re: database access sheldonlg <sheldonlg@thevillages.net> - 2011-05-26 10:22 -0400
Re: database access Jerry Stuckle <jstucklex@attglobal.net> - 2011-05-26 11:41 -0400
Re: database access sheldonlg <sheldonlg@thevillages.net> - 2011-05-26 12:07 -0400
Re: database access sheldonlg <sheldonlg@thevillages.net> - 2011-05-26 12:14 -0400
Re: database access sophal chao <w3cyberlearning@gmail.com> - 2011-05-29 13:41 -0700
Re: database access Jerry Stuckle <jstucklex@attglobal.net> - 2011-05-29 23:12 -0400
Re: database access JohnT <john@example.com> - 2011-05-26 13:05 +0000
Re: database access Jivanmukta <jivanmukta@poczta.onet.pl> - 2011-05-26 06:37 -0700
Re: database access sheldonlg <sheldonlg@thevillages.net> - 2011-05-26 10:30 -0400
Re: database access sheldonlg <sheldonlg@thevillages.net> - 2011-05-26 10:33 -0400
Re: database access Jerry Stuckle <jstucklex@attglobal.net> - 2011-05-26 11:43 -0400
Re: database access Michael Fesser <netizen@gmx.de> - 2011-05-26 19:48 +0200
Re: database access Erwin Moller <Since_humans_read_this_I_am_spammed_too_much@spamyourself.com> - 2011-05-30 10:56 +0200
csiph-web