为什么我不应该在PHP中使用mysql_ *函数?

时间:2012-10-12 13:18:39

标签: php mysql database

为什么不应该使用mysql_*函数的技术原因是什么? (例如mysql_query()mysql_connect()mysql_real_escape_string())?

即使他们在我的网站上工作,为什么还要使用别的东西?

如果它们无法在我的网站上运行,为什么会出现

等错误
  

警告:mysql_connect():没有这样的文件或目录

13 个答案:

答案 0 :(得分:1979)

MySQL扩展:

  • 未进行积极开发
  • 自PHP 5.5(2013年6月发布)以来正式deprecated
  • 自PHP 7.0(2015年12月发布)以来 removed完全
    • 这意味着从31 Dec 2018开始,它不会存在于任何受支持的PHP版本中。目前,它只获得安全性更新。
  • 缺少OO界面
  • 不支持:
    • 非阻塞,异步查询
    • Prepared statements或参数化查询
    • 存储过程
    • 多个陈述
    • 交易
    • “新”密码验证方法(默认情况下在MySQL 5.6中启用; 5.7中要求)
    • MySQL 5.1中的所有功能

由于它已被弃用,因此使用它会使您的代码不再适用于未来。

缺乏对预准备语句的支持特别重要,因为它们提供了一种更清晰,更不容易出错的方法来转义和引用外部数据,而不是通过单独的函数调用来手动转义它。

请参阅the comparison of SQL extensions

答案 1 :(得分:290)

首先,让我们从我们给大家的标准评论开始:

  

Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

让我们逐句逐句地解释:

  • 不再维护它们,并且已被正式弃用

    这意味着PHP社区正在逐渐放弃对这些非常旧的功能的支持。它们很可能不存在于PHP的未来(最新)版本中!继续使用这些函数可能会破坏你的代码(不是那么遥远)。

    新! - ext / mysql现在是 officially deprecated as of PHP 5.5!

    较新的! ext / mysql has been removed in PHP 7

  • 相反,您应该了解准备好的陈述

    mysql_*扩展程序不支持预备语句,这是(除其他外)对 SQL注入的非常有效的对策。它修复了MySQL依赖应用程序中的一个非常严重的漏洞,它允许攻击者访问您的脚本并对您的数据库执行任何可能的查询

    有关详细信息,请参阅 How can I prevent SQL injection in PHP?

  • 请参阅红盒?

    当您转到任何mysql功能手册页时,会看到一个红色框,说明它不应再使用了。

  • 使用PDO或MySQLi

    有更好,更强大且构建良好的替代方案 PDO - PHP Database Object ,它为数据库交互提供了完整的OOP方法,并且 MySQLi ,这是MySQL特定的改进。

答案 2 :(得分:138)

mysql_函数:

  1. 已过期 - 他们不再维护
  2. 不允许您轻松移动到另一个数据库后端
  3. 不支持准备好的陈述,因此
  4. 鼓励程序员使用连接来构建查询,从而导致SQL注入漏洞

答案 3 :(得分:89)

原因有很多,但也许最重要的原因是这些功能会鼓励不安全的编程实践,因为它们不支持预处理语句。准备好的语句有助于防止SQL注入攻击。

使用mysql_*函数时,您必须记住通过mysql_real_escape_string()运行用户提供的参数。如果您只是在一个地方忘记或者您碰巧只是逃避了部分输入,那么您的数据库可能会受到攻击。

PDOmysqli中使用预准备语句会使这些编程错误更难以制作。

答案 4 :(得分:72)

因为(除其他原因外)确保输入数据被消毒要困难得多。如果您使用参数化查询,就像使用PDO或mysqli一样,您可以完全避免风险。

例如,有人可以使用"enhzflep); drop table users"作为用户名。旧函数将允许每个查询执行多个语句,因此类似讨厌的bugger可以删除整个表。

如果要使用mysqli的PDO,则用户名最终会为"enhzflep); drop table users"

请参阅bobby-tables.com

答案 5 :(得分:62)

这个答案是为了说明绕过写得不好的PHP用户验证代码,如何(和使用什么)这些攻击工作以及如何用安全的预备语句替换旧的MySQL函数是多么微不足道 - 基本上,为什么StackOverflow用户(可能有很多代表)正在向新用户发出咆哮,提出问题以改进他们的代码。

首先,请随意创建这个测试mysql数据库(我已经打过我的准备工作):

mysql> create table users(
    -> id int(2) primary key auto_increment,
    -> userid tinytext,
    -> pass tinytext);
Query OK, 0 rows affected (0.05 sec)

mysql> insert into users values(null, 'Fluffeh', 'mypass');
Query OK, 1 row affected (0.04 sec)

mysql> create user 'prepared'@'localhost' identified by 'example';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on prep.* to 'prepared'@'localhost' with grant option;
Query OK, 0 rows affected (0.00 sec)

完成后,我们可以转到我们的PHP代码。

让我们假设以下脚本是网站管理员的验证过程(简化但是如果您复制并使用它进行测试则有效):

<?php 

    if(!empty($_POST['user']))
    {
        $user=$_POST['user'];
    }   
    else
    {
        $user='bob';
    }
    if(!empty($_POST['pass']))
    {
        $pass=$_POST['pass'];
    }
    else
    {
        $pass='bob';
    }

    $database='prep';
    $link=mysql_connect('localhost', 'prepared', 'example');
    mysql_select_db($database) or die( "Unable to select database");

    $sql="select id, userid, pass from users where userid='$user' and pass='$pass'";
    //echo $sql."<br><br>";
    $result=mysql_query($sql);
    $isAdmin=false;
    while ($row = mysql_fetch_assoc($result)) {
        echo "My id is ".$row['id']." and my username is ".$row['userid']." and lastly, my password is ".$row['pass']."<br>";
        $isAdmin=true;
        // We have correctly matched the Username and Password
        // Lets give this person full access
    }
    if($isAdmin)
    {
        echo "The check passed. We have a verified admin!<br>";
    }
    else
    {
        echo "You could not be verified. Please try again...<br>";
    }
    mysql_close($link);

?>

<form name="exploited" method='post'>
    User: <input type='text' name='user'><br>
    Pass: <input type='text' name='pass'><br>
    <input type='submit'>
</form>

乍一看似乎足够合法。

用户必须输入登录名和密码,对吗?

很棒,不要输入以下内容:

user: bob
pass: somePass

并提交。

输出如下:

You could not be verified. Please try again...

超!按预期工作,现在让我们尝试实际的用户名和密码:

user: Fluffeh
pass: mypass

惊人!全面的Hi-fives,代码正确验证了管理员。它非常完美!

嗯,不是真的。让我们说用户是一个聪明的小人物。让我们说这个人就是我。

输入以下内容:

user: bob
pass: n' or 1=1 or 'm=m

输出是:

The check passed. We have a verified admin!

恭喜,您刚刚允许我输入您的超级保护管理员部分,我输入了错误的用户名和假密码。说真的,如果你不相信我,用我提供的代码创建数据库,并运行这个PHP代码 - 一眼就看起来似乎确实很好地验证了用户名和密码。

所以,作为答案,那就是为什么你会被骂。

所以,让我们来看看出了什么问题,以及为什么我刚刚进入你的超级管理员蝙蝠洞。我猜了一下,并假设你没有小心你的输入,只是直接将它们传递给数据库。我以一种改变您实际运行的查询的方式构造输入。那么,它应该是什么样的,最终是什么呢?

select id, userid, pass from users where userid='$user' and pass='$pass'

这是查询,但是当我们用我们使用的实际输入替换变量时,我们得到以下结果:

select id, userid, pass from users where userid='bob' and pass='n' or 1=1 or 'm=m'

看看我如何构建我的密码&#34;那么它会先关闭密码周围的单引号,然后引入一个全新的比较?然后为了安全起见,我添加了另一个&#34;字符串&#34;这样单引号会在我们原来的代码中按预期关闭。

然而,这并不是人们现在大吼大叫,这是为了向您展示如何使您的代码更安全。

好的,那么出了什么问题,我们该如何解决?

这是一种典型的SQL注入攻击。最简单的事情之一。在攻击向量的范围内,这是一个蹒跚学步的攻击坦克 - 并且获胜。

那么,我们如何保护您的神圣管理部分并使其安全可靠?要做的第一件事就是停止使用那些非常旧的和已弃用的mysql_*函数。我知道,你按照你在网上找到的教程进行了操作,但它已经过时了,它已经过时了,在几分钟的时间里,我刚刚打破了它而没有打破汗水

现在,您可以更好地使用mysqli_PDO。我个人是PDO的忠实粉丝,所以我将在其余的答案中使用PDO。有专业人士和专家,但我个人认为专业人士远远超过了他们的意见。它可以在多个数据库引擎中移植 - 无论您使用的是MySQL还是Oracle,还是只是血腥的东西 - 只需更改连接字符串,它就具有我们想要使用的所有奇特功能,并且非常干净。我喜欢干净。

现在,让我们再看看那段代码,这次是用PDO对象编写的:

<?php 

    if(!empty($_POST['user']))
    {
        $user=$_POST['user'];
    }   
    else
    {
        $user='bob';
    }
    if(!empty($_POST['pass']))
    {
        $pass=$_POST['pass'];
    }
    else
    {
        $pass='bob';
    }
    $isAdmin=false;

    $database='prep';
    $pdo=new PDO ('mysql:host=localhost;dbname=prep', 'prepared', 'example');
    $sql="select id, userid, pass from users where userid=:user and pass=:password";
    $myPDO = $pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    if($myPDO->execute(array(':user' => $user, ':password' => $pass)))
    {
        while($row=$myPDO->fetch(PDO::FETCH_ASSOC))
        {
            echo "My id is ".$row['id']." and my username is ".$row['userid']." and lastly, my password is ".$row['pass']."<br>";
            $isAdmin=true;
            // We have correctly matched the Username and Password
            // Lets give this person full access
        }
    }

    if($isAdmin)
    {
        echo "The check passed. We have a verified admin!<br>";
    }
    else
    {
        echo "You could not be verified. Please try again...<br>";
    }

?>

<form name="exploited" method='post'>
    User: <input type='text' name='user'><br>
    Pass: <input type='text' name='pass'><br>
    <input type='submit'>
</form>

主要区别在于没有mysql_*个功能。它全部通过PDO对象完成,其次,它使用预备语句。现在,你问的是什么样的预备声明?它是一种在运行查询之前告诉数据库的方法,我们将要运行的查询是什么。在这种情况下,我们告诉数据库:&#34;嗨,我将运行一个select语句,想要id,userid并从表用户传递,其中userid是一个变量,pass也是一个变量。&#34 ;

然后,在execute语句中,我们向数据库传递一个包含它现在所需的所有变量的数组。

结果太棒了。让我们再次尝试那些用户名和密码组合:

user: bob
pass: somePass

用户未经过验证。真棒。

怎么样:

user: Fluffeh
pass: mypass

哦,我有点兴奋,它有效:检查通过了。我们有一位经过验证的管理员!

现在,让我们尝试一下聪明的小伙子会输入的数据,试图通过我们的小验证系统:

user: bob
pass: n' or 1=1 or 'm=m

这一次,我们得到以下信息:

You could not be verified. Please try again...

这就是为什么你在发布问题时被大吼大叫的原因 - 因为人们可以看到你的代码可以绕过而不用尝试。请使用此问题和答案来改进您的代码,使其更安全并使用当前的功能。

最后,这并不是说这是完美的代码。你可以做更多的事情来改进它,例如使用散列密码,确保当你在数据库中存储感知信息时,你不能用纯文本存储它,有多级验证 - 但实际上如果你只是改变你的旧注入代码,那么你将会很好地编写好的代码 - 而且你已经掌握了这一点而且还在阅读的事实让我有一种希望,你不仅会实现在编写您的网站和应用程序时,这类代码,但您可能会出去研究我刚才提到的其他内容 - 以及更多。编写尽可能最好的代码,而不是几乎不起作用的最基本代码。

答案 6 :(得分:30)

MySQL扩展是三者中最老的扩展,是开发人员用来与MySQL通信的原始方式。此扩展现在deprecated支持其他two alternatives,因为PHP和MySQL的新版本都有所改进。

  • MySQLi是使用MySQL数据库的“改进”扩展。它利用了较新版本的MySQL服务器中提供的功能,向开发人员公开了面向功能的界面和面向对象的界面,并且做了一些其他很好的事情。

  • PDO提供了一个API,它整合了以前分布在主要数据库访问扩展中的大部分功能,即MySQL,PostgreSQL,SQLite,MSSQL等。该接口公开了高级对象程序员使用数据库连接,查询和结果集,以及低级驱动程序与数据库服务器执行通信和资源处理。许多讨论和工作正在进入PDO,它被认为是在现代专业代码中使用数据库的适当方法。

答案 7 :(得分:19)

我发现上述答案非常冗长,总结一下:

  

mysqli扩展有很多   好处,关键的增强   mysql扩展名为:

     
      
  • 面向对象的界面
  •   
  • 支持准备好的陈述
  •   
  • 支持多种语句
  •   
  • 支持交易
  •   
  • 增强的调试功能
  •   
  • 嵌入式服务器支持
  •   

来源:MySQLi overview

正如上面的答案所解释的,mysql的替代品是mysqli和PDO(PHP数据对象)。

  • API支持服务器端准备语句:由MYSQLi和PDO支持
  • API支持客户端准备语句:仅由PDO支持
  • API支持存储过程:MySQLi和PDO
  • API支持多种语句和所有MySQL 4.1+功能 - 由MySQLi支持,主要由PDO支持

MySQL 5.0和PDO都是在PHP 5.0中引入的,而MySQL则是在PHP 3.0之前引入的。值得注意的是,MySQL已包含在PHP5.x中,但在以后的版本中已弃用。

答案 8 :(得分:2)

可以使用mysqli或PDO定义几乎所有mysql_*函数。只需将它们包含在旧的PHP应用程序之上,它就可以在PHP7上运行。我的解决方案here

<?php

define('MYSQL_LINK', 'dbl');
$GLOBALS[MYSQL_LINK] = null;

function mysql_link($link=null) {
    return ($link === null) ? $GLOBALS[MYSQL_LINK] : $link;
}

function mysql_connect($host, $user, $pass) {
    $GLOBALS[MYSQL_LINK] = mysqli_connect($host, $user, $pass);
    return $GLOBALS[MYSQL_LINK];
}

function mysql_pconnect($host, $user, $pass) {
    return mysql_connect($host, $user, $pass);
}

function mysql_select_db($db, $link=null) {
    $link = mysql_link($link);
    return mysqli_select_db($link, $db);
}

function mysql_close($link=null) {
    $link = mysql_link($link);
    return mysqli_close($link);
}

function mysql_error($link=null) {
    $link = mysql_link($link);
    return mysqli_error($link);
}

function mysql_errno($link=null) {
    $link = mysql_link($link);
    return mysqli_errno($link);
}

function mysql_ping($link=null) {
    $link = mysql_link($link);
    return mysqli_ping($link);
}

function mysql_stat($link=null) {
    $link = mysql_link($link);
    return mysqli_stat($link);
}

function mysql_affected_rows($link=null) {
    $link = mysql_link($link);
    return mysqli_affected_rows($link);
}

function mysql_client_encoding($link=null) {
    $link = mysql_link($link);
    return mysqli_character_set_name($link);
}

function mysql_thread_id($link=null) {
    $link = mysql_link($link);
    return mysqli_thread_id($link);
}

function mysql_escape_string($string) {
    return mysql_real_escape_string($string);
}

function mysql_real_escape_string($string, $link=null) {
    $link = mysql_link($link);
    return mysqli_real_escape_string($link, $string);
}

function mysql_query($sql, $link=null) {
    $link = mysql_link($link);
    return mysqli_query($link, $sql);
}

function mysql_unbuffered_query($sql, $link=null) {
    $link = mysql_link($link);
    return mysqli_query($link, $sql, MYSQLI_USE_RESULT);
}

function mysql_set_charset($charset, $link=null){
    $link = mysql_link($link);
    return mysqli_set_charset($link, $charset);
}

function mysql_get_host_info($link=null) {
    $link = mysql_link($link);
    return mysqli_get_host_info($link);
}

function mysql_get_proto_info($link=null) {
    $link = mysql_link($link);
    return mysqli_get_proto_info($link);
}
function mysql_get_server_info($link=null) {
    $link = mysql_link($link);
    return mysqli_get_server_info($link);
}

function mysql_info($link=null) {
    $link = mysql_link($link);
    return mysqli_info($link);
}

function mysql_get_client_info() {
    $link = mysql_link();
    return mysqli_get_client_info($link);
}

function mysql_create_db($db, $link=null) {
    $link = mysql_link($link);
    $db = str_replace('`', '', mysqli_real_escape_string($link, $db));
    return mysqli_query($link, "CREATE DATABASE `$db`");
}

function mysql_drop_db($db, $link=null) {
    $link = mysql_link($link);
    $db = str_replace('`', '', mysqli_real_escape_string($link, $db));
    return mysqli_query($link, "DROP DATABASE `$db`");
}

function mysql_list_dbs($link=null) {
    $link = mysql_link($link);
    return mysqli_query($link, "SHOW DATABASES");
}

function mysql_list_fields($db, $table, $link=null) {
    $link = mysql_link($link);
    $db = str_replace('`', '', mysqli_real_escape_string($link, $db));
    $table = str_replace('`', '', mysqli_real_escape_string($link, $table));
    return mysqli_query($link, "SHOW COLUMNS FROM `$db`.`$table`");
}

function mysql_list_tables($db, $link=null) {
    $link = mysql_link($link);
    $db = str_replace('`', '', mysqli_real_escape_string($link, $db));
    return mysqli_query($link, "SHOW TABLES FROM `$db`");
}

function mysql_db_query($db, $sql, $link=null) {
    $link = mysql_link($link);
    mysqli_select_db($link, $db);
    return mysqli_query($link, $sql);
}

function mysql_fetch_row($qlink) {
    return mysqli_fetch_row($qlink);
}

function mysql_fetch_assoc($qlink) {
    return mysqli_fetch_assoc($qlink);
}

function mysql_fetch_array($qlink, $result=MYSQLI_BOTH) {
    return mysqli_fetch_array($qlink, $result);
}

function mysql_fetch_lengths($qlink) {
    return mysqli_fetch_lengths($qlink);
}

function mysql_insert_id($qlink) {
    return mysqli_insert_id($qlink);
}

function mysql_num_rows($qlink) {
    return mysqli_num_rows($qlink);
}

function mysql_num_fields($qlink) {
    return mysqli_num_fields($qlink);
}

function mysql_data_seek($qlink, $row) {
    return mysqli_data_seek($qlink, $row);
}

function mysql_field_seek($qlink, $offset) {
    return mysqli_field_seek($qlink, $offset);
}

function mysql_fetch_object($qlink, $class="stdClass", array $params=null) {
    return ($params === null)
        ? mysqli_fetch_object($qlink, $class)
        : mysqli_fetch_object($qlink, $class, $params);
}

function mysql_db_name($qlink, $row, $field='Database') {
    mysqli_data_seek($qlink, $row);
    $db = mysqli_fetch_assoc($qlink);
    return $db[$field];
}

function mysql_fetch_field($qlink, $offset=null) {
    if ($offset !== null)
        mysqli_field_seek($qlink, $offset);
    return mysqli_fetch_field($qlink);
}

function mysql_result($qlink, $offset, $field=0) {
    if ($offset !== null)
        mysqli_field_seek($qlink, $offset);
    $row = mysqli_fetch_array($qlink);
    return (!is_array($row) || !isset($row[$field]))
        ? false
        : $row[$field];
}

function mysql_field_len($qlink, $offset) {
    $field = mysqli_fetch_field_direct($qlink, $offset);
    return is_object($field) ? $field->length : false;
}

function mysql_field_name($qlink, $offset) {
    $field = mysqli_fetch_field_direct($qlink, $offset);
    if (!is_object($field))
        return false;
    return empty($field->orgname) ? $field->name : $field->orgname;
}

function mysql_field_table($qlink, $offset) {
    $field = mysqli_fetch_field_direct($qlink, $offset);
    if (!is_object($field))
        return false;
    return empty($field->orgtable) ? $field->table : $field->orgtable;
}

function mysql_field_type($qlink, $offset) {
    $field = mysqli_fetch_field_direct($qlink, $offset);
    return is_object($field) ? $field->type : false;
}

function mysql_free_result($qlink) {
    try {
        mysqli_free_result($qlink);
    } catch (Exception $e) {
        return false;
    }
    return true;
}

答案 9 :(得分:0)

与此mysql_connect()mysql_query()类型类似的函数是PHP的先前版本(PHP 4)函数,现在尚未使用。

在最新的PHP5中,这些被mysqli_connect()mysqli_query()取代。

这就是错误背后的原因。

答案 10 :(得分:0)

MySQL在PHP 5.5.0中已弃用,在PHP 7.0.0中已删除。对于大型的旧应用程序,很难搜索和替换每个功能。

我们可以通过为下面每个正在运行的代码创建一个包装函数来使用MySQL函数。 Click here

答案 11 :(得分:-1)

这是今天(2019年1月)的一个老问题,但这可能仍然有用。大约7年前,我创建了MySQL / MySQLi / PDO功能的表格映射。可能是有用的参考。它是online here,复制如下。随时复制和粘贴HTML。

实际上,我发现将过程MySQL函数转换为OOP MySQLi是阻力最小的途径。同时打开两个数据库连接非常好,这为我们在转换过程中提供了一定的灵活性-我们可以逐个转换脚本,一次转换一个查询。尽管我今天不建议这样做,但在当时还是很方便的。

<div class="container">

<h2>Mapping Obsolete MySQL Functions to Current PHP Extensions</h2>
<table>
<tr><th>MySQL Extension</th><th>MySQL<b><i>i</i></b></th><th>PDO</th></tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-affected-rows.php">mysql_affected_rows</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.affected-rows.php">mysqli::$affected_rows</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.rowcount.php">PDOStatement::rowCount</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-client-encoding.php">mysql_client_encoding</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.character-set-name.php">mysqli::character_set_name</a></td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-close.php">mysql_close</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.close.php">mysqli::close</a></td>
    <td>Assign NULL to PDO Object</td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-connect.php">mysql_connect</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.construct.php">mysqli::__construct</a></td>
    <td><a href="http://www.php.net/manual/en/pdo.construct.php">PDO::__construct</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-create-db.php">mysql_create_db</a></td>
    <td>Query: CREATE DATABASE</a></td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-data-seek.php">mysql_data_seek</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-stmt.data-seek.php">mysqli_stmt::data_seek</a></td>
    <td>PDO::FETCH_ORI_ABS (?)</td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-db-name.php">mysql_db_name</a></td>
    <td>Query: SELECT DATABASE()</td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-db-query.php">mysql_db_query</a></td>
    <td> </td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-drop-db.php">mysql_drop_db</a></td>
    <td>Query: DROP DATABASE</td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-errno.php">mysql_errno</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.errno.php">mysqli::$errno</a></td>
    <td><a href="http://www.php.net/manual/en/pdo.errorcode.php">PDO::errorCode</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-error.php">mysql_error</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.error-list.php">mysqli::$error_list</a></td>
    <td><a href="http://www.php.net/manual/en/pdo.errorinfo.php">PDO::errorInfo</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-escape-string.php">mysql_escape_string</a></td>
    <td> </td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-fetch-array.php">mysql_fetch_array</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-result.fetch-array.php">mysqli_result::fetch_array</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.fetch.php">PDOStatement::fetch</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-fetch-assoc.php">mysql_fetch_assoc</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-result.fetch-assoc.php">mysqli_result::fetch_assoc</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.fetch.php">PDOStatement::fetch</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-fetch-field.php">mysql_fetch_field</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-result.fetch-field.php">mysqli_result::fetch_field</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.getcolumnmeta.php">PDOStatement::getColumnMeta</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-fetch-lengths.php">mysql_fetch_lengths</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-result.lengths.php">mysqli_result::$lengths</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.getcolumnmeta.php">PDOStatement::getColumnMeta</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-fetch-object.php">mysql_fetch_object</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-result.fetch-object.php">mysqli_result::fetch_object</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.fetch.php">PDOStatement::fetch</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-fetch-row.php">mysql_fetch_row</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-result.fetch-row.php">mysqli_result::fetch_row</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.fetch.php">PDOStatement::fetch</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-field-flags.php">mysql_field_flags</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-result.fetch-fields.php">mysqli_result::fetch_fields</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.getcolumnmeta.php">PDOStatement::getColumnMeta</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-field-len.php">mysql_field_len</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-result.fetch-field-direct.php">mysqli_result::fetch_field_direct</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.getcolumnmeta.php">PDOStatement::getColumnMeta</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-field-name.php">mysql_field_name</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-result.fetch-field-direct.php">mysqli_result::fetch_field_direct</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.getcolumnmeta.php">PDOStatement::getColumnMeta</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-field-seek.php">mysql_field_seek</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-result.field-seek.php">mysqli_result::field_seek</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.fetch.php">PDOStatement::fetch</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-field-table.php">mysql_field_table</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-result.fetch-field-direct.php">mysqli_result::fetch_field_direct</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.getcolumnmeta.php">PDOStatement::getColumnMeta</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-field-type.php">mysql_field_type</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-result.fetch-field-direct.php">mysqli_result::fetch_field_direct</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.getcolumnmeta.php">PDOStatement::getColumnMeta</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-free-result.php">mysql_free_result</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-result.free.php">mysqli_result::free</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.closecursor.php">PDOStatement::closeCursor</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-get-client-info.php">mysql_get_client_info</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.get-client-info.php">mysqli::get_client_info</a></td>
    <td><a href="http://www.php.net/manual/en/pdo.getattribute.php">PDO::getAttribute</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-get-host-info.php">mysql_get_host_info</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.get-host-info.php">mysqli::$host_info</a></td>
    <td><a href="http://www.php.net/manual/en/pdo.getattribute.php">PDO::getAttribute</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-get-proto-info.php">mysql_get_proto_info</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.get-proto-info.php">mysqli::$protocol_version</a></td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-get-server-info.php">mysql_get_server_info</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.get-server-info.php">mysqli::$server_info</a></td>
    <td><a href="http://www.php.net/manual/en/pdo.getattribute.php">PDO::getAttribute</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-info.php">mysql_info</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.info.php">mysqli::$info</a></td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-insert-id.php">mysql_insert_id</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.insert-id.php">mysqli::$insert_id</a></td>
    <td><a href="http://www.php.net/manual/en/pdo.lastinsertid.php">PDO::lastInsertId</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-list-dbs.php">mysql_list_dbs</a></td>
    <td>Query: SHOW DATABASES</td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-list-fields.php">mysql_list_fields</a></td>
    <td>Query: SHOW COLUMNS</td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-list-processes.php">mysql_list_processes</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.thread-id.php">mysqli::$thread_id</a></td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-list-tables.php">mysql_list_tables</a></td>
    <td>Query: SHOW TABLES</td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-num-fields.php">mysql_num_fields</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.field-count.php">mysqli::$field_count</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.columncount.php">PDOStatement::columnCount</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-num-rows.php">mysql_num_rows</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli-stmt.num-rows.php">mysqli_stmt::$num_rows</a></td>
    <td><a href="http://www.php.net/manual/en/pdostatement.rowcount.php">PDOStatement::rowCount</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-pconnect.php">mysql_pconnect</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.construct.php">mysqli::__construct</a></td>
    <td><a href="http://www.php.net/manual/en/pdo.construct.php">PDO::__construct</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-ping.php">mysql_ping</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.ping.php">mysqli::ping</a></td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-query.php">mysql_query</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.query.php">mysqli::query</a></td>
    <td><a href="http://www.php.net/manual/en/pdo.query.php">PDO::query</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-real-escape-string.php">mysql_real_escape_string</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.real-escape-string.php">mysqli::real_escape_string</a></td>
    <td><a href="http://www.php.net/manual/en/pdo.quote.php">PDO::quote</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-result.php">mysql_result</a></td>
    <td>Combination</td>
    <td><a href="http://www.php.net/manual/en/pdostatement.fetchcolumn.php">PDOStatement::fetchColumn</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-select-db.php">mysql_select_db</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.send-query.php">mysqli::send_query</a></td>
    <td><a href="http://www.php.net/manual/en/pdo.construct.php">PDO::__construct</a></td></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-set-charset.php">mysql_set_charset</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.character-set-name.php">mysqli::character_set_name</a></td>
    <td><a href="http://www.php.net/manual/en/pdo.construct.php">PDO::__construct</a></td></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-stat.php">mysql_stat</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.stat.php">mysqli::stat</a></td>
    <td><a href="http://www.php.net/manual/en/pdo.getattribute.php">PDO::getAttribute</a></td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-tablename.php">mysql_tablename</a></td>
    <td>Query: SHOW TABLES</td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-thread-id.php">mysql_thread_id</a></td>
    <td><a href="http://www.php.net/manual/en/mysqli.thread-id.php">mysqli::$thread_id</a></td>
    <td> </td>
    </tr>
<tr><td><a href="http://www.php.net/manual/en/function.mysql-unbuffered-query.php">mysql_unbuffered_query</a></td>
    <td>See <a href="http://www.php.net/manual/en/mysqlinfo.concepts.buffering.php">Buffering Concepts</a></td>
    <td> </td>
    </tr>
</table>

</div><!-- container -->

答案 12 :(得分:-7)

鉴于开发了更好的函数和代码结构,

mysql _ * 函数已被弃用(从 PHP 5.5 开始)。该功能被弃用的事实意味着不再需要在性能和安全性方面进行改进,这意味着它不太适合未来发展

如果您需要更多理由:

  • mysql _ * 函数不支持预准备语句。
  • mysql _ * 函数不支持参数绑定。
  • mysql _ * 函数缺少面向对象编程的功能。
  • 列表继续......