OOP数据库连接/断开类 [英] OOP database connect/disconnect class

查看:152
本文介绍了OOP数据库连接/断开类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习面向对象编程的概念,并建立了一个类,用于连接到数据库,选择数据库和关闭数据库连接。到目前为止一切似乎工作正常,除非关闭与数据库的连接。

I have just started learning the concept of Object oriented programming and have put together a class for connecting to a database, selecting database and closing the database connection. So far everything seems to work out okay except closing the connection to the database.

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

当我尝试断开与数据库的连接时,我收到以下错误信息:

When I attempt to disconnect from the database I get the following error message:

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

我猜这不是放置connectdb

I'm guessing it isn't as simple as placing the connectdb method within the parenthesis of the mysql_close function but can't find the right way to do it.

感谢

推荐答案

我将添加一个连接/链接变量到你的类,并使用析构函数。
这也将避免你记住关闭你的连接,因为它是自动完成。

这是$ this->链接,你需要传递给你的mysql_close()。

I would add a connection/link variable to your class, and use a destructor. That will also save you from haveing to remember to close your connection, cause it's done automatically.
It is the $this->link that you need to pass to your mysql_close().

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 />";
    }
?>

编辑:

所以人们实际上可以使用类,缺少属性/方法。

下一步将扩展查询方法,包括防止注入和任何其他帮助函数。

我做了以下更改:


So people can actually use the class, I added the missing properties/methods.
The next step would be to expand on the query method, to include protection against injection, and any other helper functions.
I made the following changes:


  • 添加了缺少的私有属性

  • 添加了__construct($ host,$ username,$ password ,$ database)

  • 将connectdb()和select()合并为__construct(),从而节省了两行代码。

  • 查询)

  • 使用示例

  • Added the missing private properties
  • Added __construct($host, $username, $password, $database)
  • Merged connectdb() and select() into __construct() saving an extra two lines of code.
  • Added query($query)
  • Example Usage

如果我输入错误,请留下建设性评论,所以我可以为其他人修复它。

Please if I made a typo or mistake, leave a constructive comment, so I can fix it for others.

这篇关于OOP数据库连接/断开类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆