DB

Direct database queries

Database queries using PEAR::DB

<?php
    $dbh = & SGL_DB::singleton();
    $query = "
        SELECT  id, gid
        FROM " . $conf['table']['user'] . "
        WHERE   username = " . $dbh->quote($input->username) . "
        AND     passwd = '" . md5($input->password) . "'
        AND     is_acct_active = 1
        AND     gid <> " . SGL_UNASSIGNED;
    $aResult = $dbh->getRow($query, DB_FETCHMODE_ASSOC);
?>

Prepared queries

using PEAR::DB

<?php
    $dbh =& SGL_DB::singleton();
    $sth = $dbh->prepare("  UPDATE " . $conf['table']['user'] . "
                            SET gid = $gid
                            WHERE id = ?");
    foreach ($aUsers as $uid => $username) {
        ''  if attempt to remove admin (uid = 1), silently ignore
        if ($uid == 1) {
            continue;
        }
        $dbh->execute($sth, $uid);
    }
?>

Adding a user object

Adding a user object with 15+ fields to permanent storage (DB_DataObject), taking into account config variables, using locale-specific time values, employing sequences for database independence

<?php
        $oUser = & new DataObjects_Usr();
        $oUser->setFrom($input->user);
        $oUser->passwd = md5($input->user->passwd);
        if ($conf['registermgr']['autoEnable']) {
            $oUser->is_acct_active = 1;
        }
        $dbh = $oUser->getDatabaseConnection();
        $oUser->id = $dbh->nextId('usr');
        $oUser->date_created = $oUser->last_updated = SGL::getTime();
        $success = $oUser->insert();
?>