如何使用已弃用的mysql_*函数成功重写旧的mysql-php代码?

时间:2022-10-17 13:25:18

I am still learning mostly from books I buy, but today I leart that my book is old even though I bought it this year concerning programming in PHP. Now I know that mysql_* commands in PHP are deprecated and should be replaced with more secure and stable prepared statements and PDO. So I put myself to rewrite all my web according to it and maybe I will need some advices from you how to do it properly and working from you all more experienced guys :)

我仍然主要从我买的书中学习,但是今天我学到了我的书是旧的,尽管我今年买了关于PHP编程的书。现在我知道PHP中的mysql_*命令已经过时,应该用更安全、更稳定的准备语句和PDO替换。所以我把我所有的网页都按照它重写,也许我需要你的一些建议,如何正确地做,以及如何从你们这些更有经验的人那里工作:)

So I will start my rewrite with only main part (connect to db and choosing DB) in here (the rest I can do on my own with google and manuals). I will write here my old script and ask you if I am doing everything right and not missing anything and I hope this could be some good manual/answer for other people as well. So lets start.

因此,我将在这里开始重写,只使用主部分(连接到db并选择db)(剩下的我可以自己使用谷歌和手册完成)。我将在这里写下我的旧脚本,并问你我是否做的每一件事都是正确的,没有遗漏任何东西,我希望这也能成为一些好的手册/答案给其他人。我们开始。

So in config I have something like this:

在配置中,我有这样的东西:

$db = new dbConn('127.0.0.1', 'root', 'pass', 'people', 'animals');

Which should be like this:

应该是这样的:

$db = new PDO('mysql:host=127.0.0.1;dbname=people;charset=UTF-8', 'root', 'pass');

Right? But when I need to choose database later should i do it without dbname=people;? But how to choose database later?

对吧?但是当我以后需要选择数据库时,我是否应该在没有dbname=people的情况下进行;?但是以后如何选择数据库呢?

Here is my one and only script to rewrite which is basic in most web projects and I hope it will bring not only me some understanding how new PDO system really works:

这是我唯一要重写的脚本,在大多数web项目中都是基本的,我希望它不仅能让我了解新的PDO系统是如何工作的:

class dbConn
{
  public function __construct($server, $user, $pass, $db_people, $db_animals)
  {    
    if (!empty($server) && !empty($user) && !empty($pass) && !empty($db_people) && !empty($db_animals))
    {
      $this->server = $server;
      $this->user =  $user;
      $this->pass = $pass;
      $this->db_people = $db_people;  
      $this->db_animals = $db_animals;  
      $this->connect(); 
    }  
    else
    {
      die("Set up connection to db");
    }
  }

  public function connect()
  {
    $this->conn = mysql_connect($this->server, $this->user, $this->pass) or die ('cannot connect to MySQL');
  }

  public function selectDb($database)
  {
    switch($database)
    {
      case 'people':
        mysql_select_db($this->db_people, $this->conn) or die ('cannot connect to database '.$this->db_people.'.');
        mysql_query("SET NAMES 'utf8'");
        break;

      case 'animals':
        mysql_select_db($this->db_animals, $this->conn) or die ('cannot connect to database '.$this->db_animals.'.');
        mysql_query("SET NAMES 'utf8'"); 
    }
  }

  public function __destruct() 
  {
    if (!empty($this->conn))
    {
      mysql_close($this->conn); 
    }
  }  
}

So from what I know from Google and Wiki - functions like public function __construct and public function __destruct() should not be needed anymore, right? The same with functions like public function connect() SO only whats left is public function selectDb($database) but i have no idea how to do this correctly without damading all connection to database. Because in rest of my code (not mentioned here) I can easily choose database by this code: $this->db->selectDb("people"); But with prepared statements I do not know if this is even possible in easy way. I hope some advices around this from you will help me and other users understand this new code better. Other parts in code you may have are eplained in this PDO Tutorial for MySQL Developers. Thank you.

我从谷歌和Wiki中了解到像公共函数__construct和公共函数__destruction()这样的函数应该不再需要了,对吧?与public function connect()等函数相同,所以只剩下public function selectDb($database),但我不知道如何在不破坏对数据库的所有连接的情况下正确地完成这个操作。因为在我的其余代码中(这里没有提到),我可以通过以下代码轻松地选择数据库:$this->db->selectDb(“people”);但在准备好的陈述中,我不知道这是否可能以简单的方式实现。我希望您的一些建议能帮助我和其他用户更好地理解这个新代码。在这个PDO教程中,对于MySQL开发人员,您可能会遇到其他代码部分。谢谢你!

2 个解决方案

#1


5  

Actually, a simple, sweet and short: Yes, not necessary any longer.

其实,简单、甜蜜、简短:是的,不再需要了。

Let's review the code not that we have lost something:

让我们回顾一下代码,而不是我们丢失了什么:

  • __construct - The constructor merely contained all the configuration. PDO has a much easier concept here, a connection string containing the most information:

    __construct——构造函数仅仅包含了所有的配置。PDO在这里有一个更简单的概念,一个包含最多信息的连接字符串:

     mysql:host=127.0.0.1;dbname=people;charset=UTF-8
    

    Also PDO provides the constructor for use ready-made, so double not necessary.

    此外,PDO还为使用现成的构造函数提供构造函数,因此不需要双重构造函数。

  • connect - The connection function is not necessary any longer as well. This is done by instantiating PDO already. You can look for exceptions, the PHP manual has an example on it's constructor page.

    连接——连接功能也不再需要了。这是通过实例化PDO来完成的。您可以查找异常,PHP手册在其构造函数页面上有一个示例。

  • selectDb - This complicated function is not needed any longer as well. Wow, the third function we can just drop because of the PDO connection string. Much power with so less characters. Cheers!

    selectDb—这个复杂的函数不再需要了。哇,第三个函数我们可以因为PDO连接字符串而下降。拥有如此少的角色的力量。干杯!

  • __destruct - The destructor. Let's be fair: MySQL did not need this as well. However with PDO we get it for free - without writing a single line of code.

    __destruct——析构函数。让我们公平一点:MySQL也不需要这个。然而,使用PDO,我们可以免费获得它——无需编写任何代码。

Looks good! You managed to migrate from that obscure database class to PDO by removing outdated code! Congratulations:

看起来不错!通过删除过时的代码,您成功地从晦涩的数据库类迁移到PDO !祝贺你:

$db = new PDO('mysql:host=127.0.0.1;dbname=people;charset=UTF-8', 'root', 'pass');

If you now think, what about if I want to have database class on my own? Well you can do that, because you can extend from PDO (yes that works!):

如果你现在想,如果我想要一个单独的数据库类呢?你可以这样做,因为你可以从PDO扩展(是的,有效!)

class DB extends PDO
{
   ... my super-new-shiny-code
}

Why you might want to do that? No idea, but maybe it's more fluent for your code. If you're looking for a better code-example, I have one at PHP/MySQL Table with Hyperlinks.

你为什么要这么做?不知道,但也许你的代码更流畅。如果您正在寻找一个更好的代码示例,我在PHP/MySQL表中有一个超链接。

#2


1  

I think the easiest way to switch the database inside your application will be:

我认为在应用程序中切换数据库最简单的方法是:

$pdo_instance->query("USE people");

and

$pdo_instance->query("USE animals");

or the maybe better (and cleaner) way might be

或者也许更好(更干净)的方法是

$db_people = new PDO('mysql:host=127.0.0.1;dbname=people;charset=UTF-8', 'root', 'pass');

and

$db_animals = new PDO('mysql:host=127.0.0.1;dbname=animals;charset=UTF-8', 'root', 'pass');

If you mark a database in your class active, you can access the data with $db_people->query() or $db_animals->query().

如果在类活动中标记一个数据库,可以使用$db_people->查询()或$db_animals->查询()访问数据。

#1


5  

Actually, a simple, sweet and short: Yes, not necessary any longer.

其实,简单、甜蜜、简短:是的,不再需要了。

Let's review the code not that we have lost something:

让我们回顾一下代码,而不是我们丢失了什么:

  • __construct - The constructor merely contained all the configuration. PDO has a much easier concept here, a connection string containing the most information:

    __construct——构造函数仅仅包含了所有的配置。PDO在这里有一个更简单的概念,一个包含最多信息的连接字符串:

     mysql:host=127.0.0.1;dbname=people;charset=UTF-8
    

    Also PDO provides the constructor for use ready-made, so double not necessary.

    此外,PDO还为使用现成的构造函数提供构造函数,因此不需要双重构造函数。

  • connect - The connection function is not necessary any longer as well. This is done by instantiating PDO already. You can look for exceptions, the PHP manual has an example on it's constructor page.

    连接——连接功能也不再需要了。这是通过实例化PDO来完成的。您可以查找异常,PHP手册在其构造函数页面上有一个示例。

  • selectDb - This complicated function is not needed any longer as well. Wow, the third function we can just drop because of the PDO connection string. Much power with so less characters. Cheers!

    selectDb—这个复杂的函数不再需要了。哇,第三个函数我们可以因为PDO连接字符串而下降。拥有如此少的角色的力量。干杯!

  • __destruct - The destructor. Let's be fair: MySQL did not need this as well. However with PDO we get it for free - without writing a single line of code.

    __destruct——析构函数。让我们公平一点:MySQL也不需要这个。然而,使用PDO,我们可以免费获得它——无需编写任何代码。

Looks good! You managed to migrate from that obscure database class to PDO by removing outdated code! Congratulations:

看起来不错!通过删除过时的代码,您成功地从晦涩的数据库类迁移到PDO !祝贺你:

$db = new PDO('mysql:host=127.0.0.1;dbname=people;charset=UTF-8', 'root', 'pass');

If you now think, what about if I want to have database class on my own? Well you can do that, because you can extend from PDO (yes that works!):

如果你现在想,如果我想要一个单独的数据库类呢?你可以这样做,因为你可以从PDO扩展(是的,有效!)

class DB extends PDO
{
   ... my super-new-shiny-code
}

Why you might want to do that? No idea, but maybe it's more fluent for your code. If you're looking for a better code-example, I have one at PHP/MySQL Table with Hyperlinks.

你为什么要这么做?不知道,但也许你的代码更流畅。如果您正在寻找一个更好的代码示例,我在PHP/MySQL表中有一个超链接。

#2


1  

I think the easiest way to switch the database inside your application will be:

我认为在应用程序中切换数据库最简单的方法是:

$pdo_instance->query("USE people");

and

$pdo_instance->query("USE animals");

or the maybe better (and cleaner) way might be

或者也许更好(更干净)的方法是

$db_people = new PDO('mysql:host=127.0.0.1;dbname=people;charset=UTF-8', 'root', 'pass');

and

$db_animals = new PDO('mysql:host=127.0.0.1;dbname=animals;charset=UTF-8', 'root', 'pass');

If you mark a database in your class active, you can access the data with $db_people->query() or $db_animals->query().

如果在类活动中标记一个数据库,可以使用$db_people->查询()或$db_animals->查询()访问数据。