Difference between revisions of "Connecting to an external database/zh-tw"

From Joomla! Documentation

(Created page with "連接到外部資料庫")
 
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
<noinclude><languages /></noinclude> If you need to access tables within the same database as your Joomla! installation then you can simply use the JFactory->getDbo method.  This uses the already established connection that Joomla! uses to connect to the database.  For example:
+
<noinclude><languages /></noinclude> 如果您要存取同樣位於 Joomla! 安裝資料庫內的資料表,那您只需要使用<tt>JFactory->getDbo</tt> method。這個作法會利用 Joomla! 原本就建立的連線。例如:
  
 
<source lang="php">
 
<source lang="php">
Line 7: Line 7:
 
</source>
 
</source>
  
$db is now an object of type [http://api.joomla.org/cms-3/classes/JDatabaseDriver.html JDatabaseDriver] and you can perform database operations on it using the usual methods.
+
這樣 $db 就會是 type [http://api.joomla.org/cms-3/classes/JDatabaseDriver.html JDatabaseDriver] 的物件,您可以使用例行的 method 來進行資料庫操作。
  
But what if you want to connect to a completely different database from the one used by Joomla!?.  This might be a different database on the same machine as your Joomla! site or it might be on a different host and perhaps even require a different database driver.  Well, you can do this using the [http://api.joomla.org/cms-3/classes/JDatabaseDriver.html#getInstance JDatabaseDriver->getInstance] method.
+
但如果您想要連接到完全部一樣的資料庫呢?有可能是和 Joomla!資料庫伺服器相同的主機上,或甚至是不同的主機磁碟上。好的,您需要使用 [http://api.joomla.org/cms-3/classes/JDatabaseDriver.html#getInstance JDatabaseDriver->getInstance] method。
  
 
<source lang="php">
 
<source lang="php">
Line 26: Line 26:
 
</source>
 
</source>
  
$db is now an object of type [http://api.joomla.org/cms-3/classes/JDatabaseDriver.html JDatabaseDriver] and you can perform database operations on it using the usual methods.
+
這樣 $db 就會是 type [http://api.joomla.org/cms-3/classes/JDatabaseDriver.html JDatabaseDriver] 的物件,您可以使用例行的 method 來進行資料庫操作。
  
Note that if the database uses a non-standard port number then this can be specified by adding it to the end of the host name.  For example, you might have your MySQL database running on port 3307 (the default is port 3306), in which case your host name might be 'db.myhost.com:3307'.
+
值得注意的是,如果這個資料庫使用非標準連接埠,那需要在 host name 後面來指定。例如,MySQL使用3307埠 (預設是 3306埠),您的 host name要設定為 'db.myhost.com:3307'
  
One feature of using [http://api.joomla.org/cms-3/classes/JDatabaseDriver.html#getInstance JDatabaseDriver->getInstance] is that if another call is made with the same parameters it will return the previously created object rather than creating a fresh one.
+
<tt>[http://api.joomla.org/cms-3/classes/JDatabaseDriver.html#getInstance JDatabaseDriver->getInstance] </tt>的功能之一是,如果另一個呼叫使用相同參數來發動,它會回傳前一個建立的,而不是新建立的那一個。
  
Note, however, that the parameters must match exactly for this to happen.  For example, if two calls were made to a MySQL database using [http://api.joomla.org/cms-3/classes/JDatabaseDriver.html#getInstance JDatabaseDriver->getInstance], with the first using a host name of 'db.myhost.com' and the second using 'db.myhost.com:3306', then two separate connections would be made, even though port 3306 is the default port for MySQL and so the parameters are logically the same.
+
然而,請注意,需要完全相符的參數才能實現,例如,如果有兩個呼叫對 MySQL 資料庫使用[http://api.joomla.org/cms-3/classes/JDatabaseDriver.html#getInstance JDatabaseDriver->getInstance],頭一個使用host name of 'db.myhost.com',第二個使用'db.myhost.com:3306';那麼會成功建立兩個不同的連線。雖然 3306 埠是MySQL的預設通訊埠。所以 and so the parameters are logically the same.
  
If you want to use the JModelLegacy with pagination etc. you can choose a different way. The point is, you have to replace the standard database object linked to the Joomla background database using JModelLegacy->setDbo. The first step is to override the constructor of your JModelLegacy in your model file
+
如果您想要在分頁之類的情境使用 <tt>JModelLegacy</tt>,您可以使用不同的方法。重點是,您需要使用<tt>JModelLegacy->setDbo</tt>,取代標準的資料庫物件連結到 Joomla! 背景。第一步驟是在model檔案中,覆寫您的<tt>JModelLegacy</tt> 結構。
 
<source lang="php">
 
<source lang="php">
 
public function __construct($config = array())
 
public function __construct($config = array())

Latest revision as of 20:36, 22 February 2021

Other languages:
English • ‎中文(台灣)‎

如果您要存取同樣位於 Joomla! 安裝資料庫內的資料表,那您只需要使用JFactory->getDbo method。這個作法會利用 Joomla! 原本就建立的連線。例如:

<?php
$db = JFactory::getDbo();
?>

這樣 $db 就會是 type JDatabaseDriver 的物件,您可以使用例行的 method 來進行資料庫操作。

但如果您想要連接到完全部一樣的資料庫呢?有可能是和 Joomla!資料庫伺服器相同的主機上,或甚至是不同的主機磁碟上。好的,您需要使用 JDatabaseDriver->getInstance method。

<?php
$option = array(); //prevent problems

$option['driver']   = 'mysql';            // Database driver name
$option['host']     = 'db.myhost.com';    // Database host name
$option['user']     = 'fredbloggs';       // User for database authentication
$option['password'] = 's9(39s£h[%dkFd';   // Password for database authentication
$option['database'] = 'bigdatabase';      // Database name
$option['prefix']   = 'abc_';             // Database prefix (may be empty)

$db = JDatabaseDriver::getInstance( $option );
?>

這樣 $db 就會是 type JDatabaseDriver 的物件,您可以使用例行的 method 來進行資料庫操作。

值得注意的是,如果這個資料庫使用非標準連接埠,那需要在 host name 後面來指定。例如,MySQL使用3307埠 (預設是 3306埠),您的 host name要設定為 'db.myhost.com:3307'。

JDatabaseDriver->getInstance 的功能之一是,如果另一個呼叫使用相同參數來發動,它會回傳前一個建立的,而不是新建立的那一個。

然而,請注意,需要完全相符的參數才能實現,例如,如果有兩個呼叫對 MySQL 資料庫使用JDatabaseDriver->getInstance,頭一個使用host name of 'db.myhost.com',第二個使用'db.myhost.com:3306';那麼會成功建立兩個不同的連線。雖然 3306 埠是MySQL的預設通訊埠。所以 and so the parameters are logically the same.

如果您想要在分頁之類的情境使用 JModelLegacy,您可以使用不同的方法。重點是,您需要使用JModelLegacy->setDbo,取代標準的資料庫物件連結到 Joomla! 背景。第一步驟是在model檔案中,覆寫您的JModelLegacy 結構。

public function __construct($config = array())
	 {
		parent::__construct($config);

		$option = array(); //prevent problems
 
		$option['driver']   = 'mysql';            // Database driver name
		$option['host']     = 'localhost';    // Database host name
		$option['user']     = 'myusername';       // User for database authentication
		$option['password'] = 'saltedpassword';   // Password for database authentication
		$option['database'] = 'db_extern';      // Database name
		$option['prefix']   = '';             // Database prefix (may be empty)
		
		$db = JDatabaseDriver::getInstance( $option );
		parent::setDbo($db);
	 }

After that JModelLegacy behaves normal but uses your database.