Home Tutorial Mengenal Database Class (Database Constructor)
jerrywijaya
E-mail
Mengenal Database Class (Database Constructor)
Tutorial
Jerry Wijaya, Thursday, 29 May 2008 10:40
Fungsi Database (Database Function) ini merupakan constructor dari Kelas Database (Database Class), dengan syntax seperti berikut ini.

void database ( string $host , string $user, string $pass, string $db , stirng $table_prefix, boolean $goOffline )


$host
    adalah URL dari sistem hosting database. Biasanya 'localhost'. Parameter ini bersifat opsional, jika dikosongkan maka defaultnya akan menggunakan 'localhost'.

$user
    adalah username yang dibutuhkan untuk konek ke database.

$pass
    adalah password dari $user yang digunakan untuk konek ke database.

$db
    adalah nama database itu sendiri.

$table_prefix
    adalah string yang menjadi imbuhan awal untuk semua nama table. Parameter ini bersifat opsional, jika dikosongkan maka defaultnya akan menggunakan 'mos_'.

$goOffline
    adalah boolean yang jika di-isi true dan terjadi error, maka web akan menampilkan pesan offline, jika di-isi false, maka akan menampilkan semua error. Defaultnya bernilai true.

Contoh penggunaan:

$host = 'localhost';
$user = 'userdb';
$pass = 'password';
$db = 'test';
$table_prefix = 'jos_';
$db = new database( $host, $user, $pass, $db, $table_prefix );



Kode lengkap fungsi database yang ada di kelas database pada file database.php adalah sebagai berikut.

function database( $host='localhost', $user, $pass, $db='', $table_prefix='', $goOffline=true ) {
    // perform a number of fatality checks, then die gracefully
    if (!function_exists( 'mysql_connect' )) {
        $mosSystemError = 1;
        if ($goOffline) {
            $basePath = dirname( __FILE__ );
            include $basePath . '/../configuration.php';
            include $basePath . '/../offline.php';
            exit();
        }
    }
    if (phpversion() < '4.2.0') {
        if (!($this->_resource = @mysql_connect( $host, $user, $pass ))) {
            $mosSystemError = 2;
            if ($goOffline) {
                $basePath = dirname( __FILE__ );
                include $basePath . '/../configuration.php';
                include $basePath . '/../offline.php';
                exit();
            }
        }
    } else {
        if (!($this->_resource = @mysql_connect( $host, $user, $pass, true ))) {
            $mosSystemError = 2;
            if ($goOffline) {
                $basePath = dirname( __FILE__ );
                include $basePath . '/../configuration.php';
                include $basePath . '/../offline.php';
                exit();
            }
        }
    }
    if ($db != '' && !mysql_select_db( $db, $this->_resource )) {
        $mosSystemError = 3;
        if ($goOffline) {
            $basePath = dirname( __FILE__ );
            include $basePath . '/../configuration.php';
            include $basePath . '/../offline.php';
            exit();
        }
    }
    $this->_table_prefix = $table_prefix;
       //@mysql_query("SET NAMES 'utf8'", $this->_resource);
    $this->_ticker = 0;
    $this->_log = array();
}

Comments
Search
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

 

JUG Indonesia

Id-joomla.com adalah situs resmi Joomla User Group (JUG) Indonesia dan Joomla Translation Team

Download Terjemahan Joomla terakreditasi dari JoomlaCode dan laporkan bugs, ide dan saran nya di Sub Forum Hanacaraka

Login

Web
ID-Joomla
Banner
Dengarkan siaran radio ID-Joomla   Chatroom ID-Joomla   Facebook Page ID-Joomla
We have 973 guests and 4 members online
Joomla! is the trademark of Open Source Matters, Inc in the United States and other countries. The Joomla! name is used under a limited license granted by Open Source Matters.
Id-joomla.com is not affiliated with or endorsed by Open Source Matters or the Joomla! Project