2012-03-10 122 views
3

我剛剛開始學習面向對象編程的概念,並將一個類連接到數據庫,選擇數據庫並關閉數據庫連接。到目前爲止,除了關閉與數據庫的連接外,一切似乎都行得通。OOP數據庫連接/斷開類

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(); 

當我試圖從數據庫中,我得到了以下錯誤消息斷開:

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in F:\Programs\webserver\root\oop\oop.php on line 53 

我猜它並不像放置connectdb方法mysql_close的括號內爲簡單功能,但無法找到正確的方法來做到這一點。

感謝

回答

12

我會添加一個連接/鏈接變量到你的類,並使用析構函數。 這也可以幫助您避免關閉連接,導致連接自動完成。
這是你需要傳遞給你的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 />"; 
    } 
?> 

編輯:
所以人們可以實際使用的類,我添加缺少的屬性/方法。
下一步是擴展查詢方法,包括防止注入和其他輔助函數。
我做了以下修改:

  • 添加缺少的私有財產
  • 新增__construct($主機,$的用戶名,$密碼,$數據庫)
  • 合併connectdb()和select()爲__construct ()保存額外的兩行代碼。
  • 新增查詢($查詢)
  • 使用示例

如有我犯了一個錯字或錯誤,留下一個建設性的意見,這樣我就可以修復它爲別人着想。

2

你不從connectdb()返回任何東西。但你通過這個函數的返回到mysql_close()

+1

順便說一句,如果你打算這樣做,你可能還需要爲類中的數據庫連接保留一個真正的資源。 – 2012-03-10 23:23:38

+0

當你說在類中保持數據庫連接的真實來源時,你是什麼意思? – crm 2012-03-10 23:35:42

+0

@crm:添加一個私有成員並將其存儲在那裏,如'$ this-> connection'。 – hakre 2012-03-10 23:40:01

1

mysql_close需要一個參數來斷開連接,但是你什麼也沒有提供。

 

class Database { 

    private $host, $username, $password, $con; 

    public function __construct($ihost, $iusername, $ipassword){ 
     $this->host = $ihost; 
     $this->username = $iusername; 
     $this->password = $ipassword; 
     $this->con = false; 
    } 


    public function connect() { 
     $connect = mysql_connect($this->host, $this->username, $this->password); 
     return $connect; 
    } 


    public function connectdb(){ 
     $conn = $this->connect(); 
     if($conn) 
     { 
      $this->con = true; 
      echo "Successsfully Connected. 
"; return true; } else { echo "Sorry Could Not Connect.
"; return false; } } public function select($database){ if($this->con) { if(mysql_select_db($database)) { echo "Successfully Connected Database. $database.
"; return true; } else { echo "Unknown database.
"; } } else { echo "No active Connection.
"; return false; } } public function disconnectdb(){ if($this->con) { if(mysql_close($this->connect())) { $this->con = false; echo "Successfully disconnected.
"; return true; } } else { echo "Could Not disconnect.
"; return false; } } } $database = new database('localhost', 'root', ''); $database->connectdb(); $database->select('databaseoffacebook'); $database->disconnectdb();
2

您應該意識到,mysql_*功能是在PHP 4中引入的,該功能早於1年前。這個API是非常古老的,並且該過程已經開始實際上deprecating this extension

你不應該在2012年用mysql_*函數寫新代碼。

存在兩個很好的替代方案:PDOMySQLi。這兩種方法都是用面向對象的代碼編寫的,而且它們還使您能夠使用prepared statements

那比如你在PDO寫是這樣的原帖表明:

//connect to the the database 
$connection = new PDO('mysql:host=localhost;dbname=msm', 'username', 'password'); 
//disconnects 
$connection = null; 

當然也有較爲複雜的情況,但問題的立場 - 時間去發展。

0

面向對象編程適用於PDO和mysqli。試一試

-1
<?php 

class Database{ 

    private $link; 
    //private $host,$username,$password,$database; 
    //private $status; 

    public function __construct(){ 

     $this->host   = 'localhost'; 
     $this->username  = 'root'; 
     $this->password  = ''; 
     $this->database  = 'workclass'; 

     $this->link = mysqli_connect($this->host,$this->username,$this->password); 

     $this->status = mysqli_select_db($this->link,$this->database); 

     if (!$this->status) { 
      return $this->status="Failed to Connected with Database"; 
     }else{ 
      return $this->status="Database is connected"; 
     } 
    } 
} 

$object = new Database(); 

echo $object->status; 

?> 
+0

如果你解釋你的答案不只是放置一堆代碼,這將是很好的。 – 2017-01-11 06:40:13

+0

我只是給出了我對上述問題的回答(「我剛剛開始學習面向對象編程的概念,並將一個類連接到數據庫,選擇數據庫並關閉數據庫連接。」),並且我們已經使用了這個代碼綠色刻度標記答案,我們遇到了問題,所以我們重新創建了這個程序,並給出我的答案。謝謝 – 2017-01-12 06:25:38

相關問題