CodeIgniter 使用手冊版本 2.2.0


連接您的資料庫

有兩種方法可以連接資料庫:

自動連接

"自動連接"就是將每個頁面都載入讀取資料庫類別. 要啟用自動連接,請修改底下檔案,增加 database 字串到 library 陣列裡面:

application/config/autoload.php

手動連接

假如只有部份網頁需要用到資料庫連接,您可以增加底下這段程式碼到需要的函式裡面,或者是可以在建構子(constructor)宣告給該類別全部函式使用.

$this->load->database();

假如上述函式第一個參數不包含任何資訊,它將會在系統指定的資料庫檔案中尋找。對於大多數人來說,這是第一首選方法。

可用參數

  1. 資料庫連接設定值,用陣列或者是 DSN 字串表示.
  2. TRUE/FALSE (boolean). 是否回傳連線 ID 值(請繼續閱讀底下多重資料庫連接方式).
  3. TRUE/FALSE (boolean). 是否啟用 Active Record 類別。預設值為 TRUE.

手動連接到一個資料庫

第一個參數是可以選擇性的設定,可以從您的設定檔裡面指定要連接的名稱,或者是您可以自行定義資料庫參數,此設定參數並非在您的設定檔裡面. 範例:

從檔案裡面選擇指定的 group,您可以使用下面方式:

$this->load->database('group_name');

group_name 指的是從您的設定檔案裡面所存在的連接資料庫名稱.

要手動連接資料庫,可以指定底下參數值來呈現:

$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";

$this->load->database($config);

想要瞭解每個參數的屬性,可以參考 資料庫設定頁面.

或者是您可以以 Data Source Name 來連接資料庫。DSNs 設定方式必需如下:

$dsn = 'dbdriver://username:password@hostname/database';

$this->load->database($dsn);

為了覆蓋系統預設值,當使用 DSN 字串來連接資料庫時,可以增加設定值為查詢字串。

$dsn = 'dbdriver://username:password@hostname/database?char_set=utf8&dbcollat=utf8_general_ci&cache_on=true&cachedir=/path/to/cache';

$this->load->database($dsn);

連接多重資料庫

假如想要同時使用多個資料庫,您可以使用底下方式:

$DB1 = $this->load->database('group_one',TRUE);
$DB2 = $this->load->database('group_two',TRUE);

注意: 更改上面字串 "group_one" 和 "group_two" 為您所要指定的連接資料庫名稱(或者是您也可以使用如上所述的參數連接值).

藉由設定第二個參數為 TRUE (boolean) 此函式將會回傳資料庫物件(database object).

當您使用此方法,您將會使用物件名稱來取代原本使用的方法。換句話說,不是使用下面方式:

$this->db->query();
$this->db->result();
etc...

您將代替使用:

$DB1->query();
$DB1->result();
etc...

Reconnecting / Keeping the Connection Alive

If the database server's idle timeout is exceeded while you're doing some heavy PHP lifting (processing an image, for instance), you should consider pinging the server by using the reconnect() method before sending further queries, which can gracefully keep the connection alive or re-establish it.

$this->db->reconnect();

Manually closing the Connection

While CodeIgniter intelligently takes care of closing your database connections, you can explicitly close the connection.

$this->db->close();