PHP面向对象编程与MySQL编程

7

我是PHP编程的新手,希望能够得到一些帮助。请看下面的代码:


PHP代码

<?php
class Account
{
  public function register()
  {
    $db_link = mysql_connect("localhost","root","");  // Create Connection
    if (!$db_link)  // Check connection
    {
      die(mysql_error());
    }

    mysql_close($db_link); // Close Connection
  }

  public function login()
  {
    $con = mysql_connect("localhost","root","")  // create connection
    if (!$con)  // create connection
    {
      die(mysql_error());
    }
    mysql_close($con); //close connection
  }

}
?>

我的问题是,为每个对象方法创建独立的数据库链接是否是最好的选择?还有更好或替代的方法吗?希望我已经解释得足够清楚。


以下内容是否正确?

$x = new Account("localhost", "root", "");

-并且x将拥有自己的连接...完成后就关闭?

2
在整个程序的开头和结尾只需使用mysql_connect()mysql_close()即可。没有必要在每个方法中实例化连接。 - ldiqual
@ldiqual:你的评论可以作为答案! - Tamer Shlash
4
@idiqual,你不应该鼓励使用mysql_,特别是对于刚接触这门语言并开始新项目的人。 - Charles Sprayberry
1
如果您想将db连接到会计类中,请在__construct中进行连接,并实现__destruct方法以在对象销毁时销毁连接。 - dm03514
使用单例模式。 例如:https://dev59.com/al7Va4cB1Zd3GeqPM8nU#8685944 - Shiplu Mokaddim
5个回答

10

我不建议以这种方式创建数据库连接。只需创建一个连接并将其注入到使用它的对象中即可。您不应该为每个对象创建新的连接。

代码示例:

$connection = new mysqli('localhost', 'user', 'password');

$Account = new Account($connection);

需要将Account更改为以下形式:

class Account {

    protected $connection;

    public function __construct(mysqli $connection) {
        $this->connection = $connection;
    }

    public function register() {
        // use $this->connection for db
    }

    public function login() {
        // use $this->connection for db
    }

}

我建议您查看关于选择MySQL API的php.net文档。如果您真的想在PHP和MySQL中使用OOP,您需要切换到mysqliPDO,因为您正在使用的API不支持真正的OOP接口。

你忘记关闭连接了。 - Gabriel Santos
4
@GabrielSantos 不,我没有故意关闭它。如果还有其他对象需要使用该连接怎么办?那么我们就必须再次打开连接。创建连接对象的代码应负责关闭连接。 - Charles Sprayberry

3
我建议:
public function __construct(mysqli $connection) {
    $this->connection = $connection;
    if(!$this->$connection) {
        die(mysql_error());
    }
}

public function __destruct() {
    mysql_close($this->$connection);
}

我不确定我会建议在这种(或任何OO)情况下使用die。 - cmbuckley
@cbuckley 这只是为了示例目的。 - Gabriel Santos
不是的。die与OOP没有关系。 - Gabriel Santos
1
异常处理是面向对象编程的一个重要组成部分。die()函数会立即产生致命错误。请参阅http://stackoverflow.com/questions/3845494/php-oop-exceptions-or-die。 - cmbuckley
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/6325/discussion-between-cbuckley-and-gabriel-santos - cmbuckley
显示剩余2条评论

0

您可以使用一些代码,例如:

$db_link = mysql_connect("localhost","root","");  // Create Connection
if (!$db_link)  // Check connection 
{
  die(mysql_error());
}
mysql_select_db("db_name");
$q=mysql_query("SELECET * FROM table_name LIMIT 1");

其他查询将会在这里

mysql_close($db_link); // Close Connection

每个页面创建一个单一连接就足够了。多于一个连接或者断开与 SQL Server 的连接并重新登录可能会导致性能降低。


-1

1
我不会使用静态类或单例模式。 - PeeHaa
1
单例模式不应该被贬低。没有理由通过静态方式将数据库连接引入全局范围。 - Charles Sprayberry
@CharlesSprayberry 再次同意。 - Gabriel Santos

-1
如果你想要将它缩小并使其更整洁/更易管理,你可以把mysql连接代码放到它自己的方法中,并像这样调用它:
<?php
class Account
{
  private $connection;

  private function connect()
  {
    $this->$connection = mysql_connect("localhost","root","");  // Create Connection
  }

  public function register()
  {
    $this->connect();
    if (!$this->$connection)  // Check connection
    {
      die(mysql_error());
    }

    mysql_close($this->$connection); // Close Connection
  }

  public function login()
  {
    $this->connect();
    if (!$this->$connection)  // create connection
    {
      die(mysql_error());
    }
    mysql_close($this->$connection); //close connection
  }

}
?>

1
因为每次调用函数时仍然会创建数据库连接,所以被踩了。 - Charles Sprayberry
@CharlesSprayberry 我同意你的观点。 - Gabriel Santos
@CharlesSprayberry 我误解了原始问题。 - George Reith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接