简体   繁体   English

PHP OOP MySQL编程

[英]PHP OOP MySQL Programming

I am a beginner in PHP programming and would like help with a little question. 我是PHP编程的初学者,想要一些小问题的帮助。 Please take a look at the code below: 请看下面的代码:


PHP Code 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
  }

}
?>

My question is if creating individual db links for every single one of the object's methods is the best way to go? 我的问题是,如果为对象的每一个方法创建单独的数据库链接是最好的方法吗? Is there a better or alternative way to do this? 有没有更好或替代的方法来做到这一点? Hopefully I've explained well enough. 希望我已经解释得很好了。


Would the following be correct? 以下是正确的吗?

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

-and x would have its own connection...and then close when its done? -and将有自己的连接...然后在完成后关闭?

I would not advise creating your database connections this way. 我不建议以这种方式创建数据库连接。 Create one connection and inject that into the object using it. 创建一个连接并使用它将其注入对象。 You should not need to create a new connection for every object. 您不需要为每个对象创建新连接。

Code example: 代码示例:

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

$Account = new Account($connection);

Would need to change Account to look like: 需要更改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
    }

}

I would also suggest that you take a look at the php.net docs about choosing a MySQL API . 我还建议您查看有关选择MySQL APIphp.net文档 If you really want to use OOP with PHP and MySQL you will need to swap over to mysqli or PDO as the API you are using does not truly support an OOP interface. 如果您真的想在PHP和MySQL中使用OOP,则需要切换到mysqliPDO因为您使用的API并不真正支持OOP接口。

I suggest: 我建议:

public function __construct(mysqli $connection) {
    $this->connection = $connection;
    if(!$this->$connection) {
        die(mysql_error());
    }
}

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

You can use some codes like as: 你可以使用一些代码,如:

$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");

Other queries would be here 其他疑问将在这里

mysql_close($db_link); // Close Connection

Creating single connection at each page is enough. 在每个页面创建单个连接就足够了。 more than one connection or disconnecting from sql server and loginning again may cause reduced performance. 多个连接或从sql server断开连接并再次登录可能会导致性能降低。

You can use a static member and all your objects will share one database connection. 您可以使用静态成员,所有对象将共享一个数据库连接。

See also PHP - a DB abstraction layer use static class vs singleton object? 另请参阅PHP - 数据库抽象层使用静态类与单例对象?

If you wanted to shrink it and keep it neater/more manageable you could put the mysql connect code into it's own method and call it like so: 如果你想缩小它并保持它更整洁/更易于管理,你可以将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
  }

}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM