FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » database access
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: database access [message #174261 is a reply to message #174190] Sun, 29 May 2011 20:41 Go to previous messageGo to previous message
sophal chao is currently offline  sophal chao
Messages: 1
Registered: May 2011
Karma:
Junior Member
On May 25, 5:37 pm, Jerry Stuckle <jstuck...@attglobal.net> 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.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

You can connect use singleton. I think this link is good for basic
tutorial.

http://www.w3cyberlearning.com/tutorial/php_pdomysql.php
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: a more elegant way?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 29 17:15:52 GMT 2024

Total time taken to generate the page: 0.04812 seconds