我剛剛開始學(xué)習(xí)面向?qū)ο缶幊痰母拍?并將一個用于連接數(shù)據(jù)庫,選擇數(shù)據(jù)庫和關(guān)閉數(shù)據(jù)庫連接的類組合在一起.到目前為止,除了關(guān)閉與數(shù)據(jù)庫的連接外,一切似乎都沒問題.
class Database { private $host, $username, $password; public function __construct($ihost, $iusername, $ipassword){ $this->host = $ihost; $this->username = $iusername; $this->password = $ipassword; } public function connectdb(){ mysql_connect($this->host, $this->username, $this->password) OR die("There was a problem connecting to the database."); echo 'successfully connected to database<br />'; } public function select($database){ mysql_select_db($database) OR die("There was a problem selecting the database."); echo 'successfully selected database<br />'; } public function disconnectdb(){ mysql_close($this->connectdb()) OR die("There was a problem disconnecting from the database."); }}$database = new database('localhost', 'root', 'usbw');$database->connectdb();$database->select('msm');$database->disconnectdb();
當(dāng)我嘗試斷開與數(shù)據(jù)庫的連接時,我收到以下錯誤消息:
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in F:\Programs\webserver\root\oop\oop.php on line 53
我猜這不像在mysql_close函數(shù)的括號中放置connectdb方法那么簡單,但找不到正確的方法.
謝謝
解決方法:
我會在你的類中添加一個連接/鏈接變量,并使用析構(gòu)函數(shù).
這也將使您不必忘記關(guān)閉連接,因為它會自動完成.
您需要傳遞給mysql_close()的是$this->鏈接.
class Database { private $link; private $host, $username, $password, $database; public function __construct($host, $username, $password, $database){ $this->host = $host; $this->username = $username; $this->password = $password; $this->database = $database; $this->link = mysql_connect($this->host, $this->username, $this->password) OR die("There was a problem connecting to the database."); mysql_select_db($this->database, $this->link) OR die("There was a problem selecting the database."); return true; } public function query($query) { $result = mysql_query($query); if (!$result) die('Invalid query: ' . mysql_error()); return $result; } public function __destruct() { mysql_close($this->link) OR die("There was a problem disconnecting from the database."); }}
用法示例:
<?php $db = new Database("localhost", "username", "password", "testDatabase"); $result = $db->query("SELECT * FROM students"); while ($row = mysql_fetch_assoc($result)) { echo "First Name: " . $row['firstname'] ."<br />"; echo "Last Name: " . $row['lastname'] ."<br />"; echo "Address: " . $row['address'] ."<br />"; echo "Age: " . $row['age'] ."<br />"; echo "<hr />"; }?>
編輯:
所以人們實際上可以使用這個類,我添加了缺少的屬性/方法.
下一步是擴(kuò)展查詢方法,包括注入保護(hù)和任何其他幫助函數(shù).
我做了以下更改:
>添加了缺少的私有屬性
>添加了__construct($host,$username,$password,$database)
>將connectdb()和select()合并到__construct()中,以節(jié)省額外的兩行代碼.
>添加了查詢($query)
>示例用法
如果我寫了一個錯字或錯誤,請留下建設(shè)性的評論,以便我可以為其他人解決.
編輯23/06/2018
正如所指出的那樣,mysql已經(jīng)過時了,因為這個問題仍然定期訪問,我想我會發(fā)布一個更新的解決方案.
class Database { private $mysqli; private $host, $username, $password, $database; /** * Creates the mysql connection. * Kills the script on connection or database errors. * * @param string $host * @param string $username * @param string $password * @param string $database * @return boolean */ public function __construct($host, $username, $password, $database){ $this->host = $host; $this->username = $username; $this->password = $password; $this->database = $database; $this->mysqli = new mysqli($this->host, $this->username, $this->password) OR die("There was a problem connecting to the database."); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $this->mysqli->select_db($this->database); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } return true; } /** * Prints the currently selected database. */ public function print_database_name() { /* return name of current default database */ if ($result = $this->mysqli->query("SELECT DATABASE()")) { $row = $result->fetch_row(); printf("Selected database is %s.\n", $row[0]); $result->close(); } } /** * On error returns an array with the error code. * On success returns an array with multiple mysql data. * * @param string $query * @return array */ public function query($query) { /* array returned, includes a success boolean */ $return = array(); if(!$result = $this->mysqli->query($query)) { $return['success'] = false; $return['error'] = $this->mysqli->error; return $return; } $return['success'] = true; $return['affected_rows'] = $this->mysqli->affected_rows; $return['insert_id'] = $this->mysqli->insert_id; if(0 == $this->mysqli->insert_id) { $return['count'] = $result->num_rows; $return['rows'] = array(); /* fetch associative array */ while ($row = $result->fetch_assoc()) { $return['rows'][] = $row; } /* free result set */ $result->close(); } return $return; } /** * Automatically closes the mysql connection * at the end of the program. */ public function __destruct() { $this->mysqli->close() OR die("There was a problem disconnecting from the database."); }}
用法示例:
<?php $db = new Database("localhost", "username", "password", "testDatabase"); $result = $db->query("SELECT * FROM students"); if(true == $result['success']) { echo "Number of rows: " . $result['count'] ."<br />"; foreach($result['rows'] as $row) { echo "First Name: " . $row['firstname'] ."<br />"; echo "Last Name: " . $row['lastname'] ."<br />"; echo "Address: " . $row['address'] ."<br />"; echo "Age: " . $row['age'] ."<br />"; echo "<hr />"; } } if(false == $result['success']) { echo "An error has occurred: " . $result['error'] ."<br />"; }?>
來源:https://www.icode9.com/content-2-323701.html