如何成功地重写旧的mysql-php代码与已弃用的mysql_ *函数? [英] How to successfully rewrite old mysql-php code with deprecated mysql_* functions?

查看:199
本文介绍了如何成功地重写旧的mysql-php代码与已弃用的mysql_ *函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然主要从我购买的书籍学习,但今天我发现我的书是旧的,即使我今年买了它的PHP编程。现在我知道PHP中的mysql_ *命令已被弃用,应该用更安全和稳定的预编译语句和PDO替代。所以我让自己根据它重写我的所有网页,也许我需要你的一些建议如何正确地工作,并从你所有更多的经验丰富的家伙:)

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 :)

所以我将开始我的重写,只有主要部分(连接到数据库和选择数据库)在这里(其余我可以做的自己的谷歌和手册)。我会在这里写我的旧脚本,并问你,如果我做一切正确,没有任何东西,我希望这可能是一些很好的手册/答案为其他人以及。

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.

所以在配置中我有这样的:

So in config I have something like this:

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

这应该是这样:

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

对吧?但是当我需要选择数据库后,我应该没有 dbname = people; ?但是如何选择数据库呢?

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

这里是我唯一的脚本重写,这是大多数Web项目的基本,我希望它不仅会带来一些理解如何新的PDO系统真的有效:

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:

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

Google和Wiki - 像 public function __construct public function __destruct()的函数不应该再需要了,对吗?与 public function connect()的功能相同只剩下的是 public function selectDb($ database)不知道如何正确地做到这一点,而不损害数据库的所有连接。因为在我的代码的其余部分(这里没有提到)我可以很容易地通过这个代码选择数据库: $ this-> db-> selectDb(people); 与准备的声明我不知道这是否甚至可能以轻松的方式。我希望你的一些建议,将帮助我和其他用户更好地了解这个新的代码。您可能拥有的代码中的其他部分在此 MySQL开发人员PDO教程中受到介绍。谢谢。

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.

推荐答案

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

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

让我们来看看代码,而不是我们失去了什么:

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


  • __ construct - 构造函数仅包含所有配置。 PDO在这里有一个更简单的概念,一个包含大多数信息的连接字符串:

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


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




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

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

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.

selectDb - 此复杂函数不再需要。哇,第三个函数我们可以因为PDO连接字符串而丢弃。许多力量与如此少的字符。干杯!

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!

__ destruct - 析构函数。让我们公平:MySQL不需要这个。但是使用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.

看起来不错!您设法通过删除过时的代码从这个模糊的数据库类迁移到PDO!恭喜:

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

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

如果你现在想,如果我想要自己的数据库类呢?你可以这样做,因为你可以从PDO扩展(是的!工作!):<​​/ p>

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!):

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

为什么你可能想这样做?不知道,但也许它更流利的你的代码。如果您正在寻找更好的代码示例,我在 PHP / MySQL表与超链接

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.

这篇关于如何成功地重写旧的mysql-php代码与已弃用的mysql_ *函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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