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

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

问题描述

我仍然主要从我买的书上学习,但今天我发现我的书已经旧了,尽管我今年买了它关于 PHP 编程.现在我知道 PHP 中的 mysql_* 命令已被弃用,应该用更安全、更稳定的准备语句和 PDO 代替.所以我让自己根据它重写我所有的网站,也许我需要你的一些建议如何正确地做到这一点,并从你们所有更有经验的人那里工作:)

所以我将在这里开始重写主要部分(连接到数据库并选择数据库)(其余的我可以使用谷歌和手册自行完成).我将在这里写下我的旧脚本并询问您我是否做对了一切并且没有遗漏任何东西,我希望这对其他人也可以成为一些很好的手册/答案.那么让我们开始吧.

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

$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; 吗?但是以后怎么选择数据库呢?

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

class dbConn{公共函数 __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();}别的{die("建立与数据库的连接");}}公共函数连接(){$this->conn = mysql_connect($this->server, $this->user, $this->pass) or die ('cannot connect to MySQL');}公共函数 selectDb($database){开关($数据库){案例人":mysql_select_db($this->db_people, $this->conn) 或 die ('无法连接到数据库 '.$this->db_people.'.');mysql_query("SET NAMES 'utf8'");休息;案例动物":mysql_select_db($this->db_animals, $this->conn) or die ('无法连接到数据库'.$this->db_animals.'.');mysql_query("SET NAMES 'utf8'");}}公共函数 __destruct(){如果 (!empty($this->conn)){mysql_close($this->conn);}}}

因此,根据我从 Google 和 Wiki 中了解到的信息 - 不应再需要 public function __constructpublic function __destruct() 之类的函数,对吧?与像 public function connect() 这样的函数一样,所以只剩下 public function selectDb($database) 但我不知道如何在不破坏所有连接的情况下正确执行此操作到数据库.因为在我的其余代码(此处未提及)中,我可以通过以下代码轻松选择数据库: $this->db->selectDb("people"); 但是对于准备好的语句,我不会知道这是否甚至可能以简单的方式实现.我希望你的一些建议将帮助我和其他用户更好地理解这个新代码.您可能拥有的代码中的其他部分在此 面向 MySQL 开发人员的 PDO 教程中进行了说明.谢谢.

解决方案

实际上,一个简单、甜蜜和简短的:是的,不再需要了.

让我们回顾一下代码,而不是我们丢失了一些东西:

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

    <块引用>

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

    另外 PDO 提供了现成的构造函数供使用,所以不需要 double.

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

  • selectDb - 这个复杂的函数也不再需要了.哇,由于 PDO 连接字符串,我们可以删除第三个函数.用这么少的角色来获得强大的力量.干杯!

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

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

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

如果你现在想,如果我想自己拥有数据库类呢?嗯,你可以这样做,因为你可以从 PDO 扩展(是的,有效!):

class DB 扩展 PDO{...我的超级新闪亮代码}

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

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

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?

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

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 - 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 - 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 - 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 - 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.

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

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
}

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_* 函数成功重写旧的 mysql-php 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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