任何人都可以請示例代碼來指導我使用單例類在php中建立數據庫連接。使用單例類在php中建立數據庫連接
2
A
回答
3
class DatabaseSingleton
{
// [Singleton]
private static $instance = null;
public static function getInstance()
{
if (!self::$instance)
{
self::$instance = new self();
}
return self::$instance;
}
private function __clone(){}
// [/Singleton]
private $connection = null;
private function __construct()
{
$this->connection = mysql_connect('localhost','root','admin');
if ($this->connection)
{
mysql_select_db('my_database');
}
}
//
// crud operations go here.
//
}
$db = DatabaseSingleton::getInstance();
$db->SomeCRUDOperation();
也許這樣的事?非常基本,但應該給你一個出發點。
+0
構造函數應該是私人的 –
+0
確實,好抓。 –
1
這是一個單例模式的樣子:
<?php
class SingletonClass
{
static private $instance = null;
static public function getInstance()
{
if (null === self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
private function __construct(){}
private function __clone(){}
}
$singletonClass = SingletonClass::getInstance();
現在你可以把隨機的功能和參數在那裏處理您的DB-東西。我希望能回答你的問題。
1
請參閱用戶手冊上如何實現Singleton模式的一個例子:http://www.php.net/manual/en/language.oop5.patterns.php
然後,只需建立你的類構造函數的數據庫連接。
0
我用的是這樣的:
class DBConn
{
static private $_db = null; // The same PDO will persist from one call to the next
private function __construct() {} // disallow calling the class via new DBConn
private function __clone() {} // disallow cloning the class
/**
* Establishes a PDO connection if one doesn't exist,
* or simply returns the already existing connection.
* @return PDO A working PDO connection
*/
static public function getConnection()
{
if (self::$_db == null) { // No PDO exists yet, so make one and send it back.
try {
self::$_db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
} catch (PDOException $e) {
// Use next line for debugging only, remove or comment out before going live.
// echo 'PDO says: ' . $e->getMessage() . '<br />';
// This is all the end user should see if the connection fails.
die('<h1>Sorry. The Database connection is temporarily unavailable.</h1>');
} // end PDO connection try/catch
return self::$_db;
} else { // There is already a PDO, so just send it back.
return self::$_db;
} // end PDO exists if/else
} // end function getConnection
} // end class DBConn
/**
* And you can use it as such in a class
* */
class Post {
public function __construct(){
$this->db = DBConn::getConnection();
}
public function getPosts()
{
try {
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM posts";
foreach ($this->_dbh->query($sql) as $row) {
var_dump($row);
}
/*** close the database connection ***/
$this->_dbh = null;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}
0
我使用類似下面
class Database
{
private $_connection;
private static $_instance; //The single instance
private $_host = "HOST";
private $_username = "USERNAME";
private $_password = "PASSWORd";
private $_database = "DATABASE";
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}
private function __construct() {
$this->_connection = new mysqli($this->_host, $this->_username,
$this->_password, $this->_database);
// Error handling
if(mysqli_connect_error()) {
trigger_error("Failed to conencto to MySQL: " . mysqli_connect_error(),
E_USER_ERROR);
}
}
// Magic method clone is for prevent duplication of connection
private function __clone() { }
public function getConnection() {
return $this->_connection;
}
}
$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query = "SELECT foo FROM etc";
$result = $mysqli->query($sql_query);
相關問題
- 1. PHP單例數據庫連接模式
- 2. 無法建立連接到MySQL數據庫使用PHP
- 3. 如何使用DataContext類建立與Oracle數據庫的連接?
- 4. 使用TLS在PHP中建立連接
- 5. 如何在PHP Laravel Lumen中建立與數據庫的連接?
- 6. 如何使用JDBC API在Java中建立數據庫連接?
- 7. 哪種方法可以在類中建立數據庫連接?
- 8. PHP類數據庫連接
- 9. 無法使用Mysql在PHP中建立與數據庫的連接
- 10. 通過PHP的MySQL數據庫連接的單個實例類
- 11. 在php中使用函數來創建數據庫連接?
- 12. 在mysql數據庫和php之間建立本地連接
- 13. WordPress的 - 建立數據庫連接
- 14. WordPress錯誤建立數據庫連接
- 15. 建立與數據庫的連接
- 16. JDBC建立與數據庫的連接
- 17. 不能建立數據庫連接
- 18. Clojure建立多個數據庫連接
- 19. 如何在Silverlight應用程序中建立數據庫連接?
- 20. joomla:使用joomla db函數建立數據庫連接
- 21. 如何在PHP類中使用連接到數據庫?
- 22. PHP類中的數據庫連接
- 23. 如何建立數據庫連接無法建立網站
- 24. 當使用以下代碼在PHP中使用PHP創建數據庫時,我們在哪裏建立連接,以及在哪裏創建數據庫?
- 25. 建立從Eclipse到PHP數據庫文件的連接
- 26. Google-App-Engine [PHP]:嘗試建立數據庫連接時出錯
- 27. 我應該與PHP建立數據庫連接嗎包括
- 28. 無法建立與PHP數據庫連接
- 29. ADO.NET EF - 無法使用VS2010建立數據庫連接
- 30. 如何使用TCP建立與數據庫的連接?
什麼「使用單例類建立在PHP數據庫連接」的元素,你所要求的? – Griwes
可能重複[誰需要單身?](http://stackoverflow.com/questions/4595964/who-needs-singletons) – Gordon