如何将使用 mysql_ 函数的脚本转换为使用 mysqli_ 函数?

时间:2023-03-05
本文介绍了如何将使用 mysql_ 函数的脚本转换为使用 mysqli_ 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

是否使用 mysqli_ 超出了这个问题的范围.考虑使用 PDO.

Whether or not to use mysqli_ is outside the scope of this question. Consider using PDO.

需要采取哪些步骤才能将脚本从使用已弃用的mysql_ 函数转换为mysqli_?

What steps need to be taken to convert a script from using the deprecated mysql_ functions to mysqli_?

在使用 mysqli_ 而不是 mysql 时,有什么需要做的不同吗?

Is there anything that needs to be done differently when using mysqli_ instead of mysql?

这是一个使用 mysql_ 函数的基本脚本:

Here's a basic script using mysql_ functions:

<?php

//define host, username and password

$con = mysql_connect($host,$username,$password);
if (!$con) {
    die('Could not connect: ' . mysql_error());
}

$db_name ="db1";
mysql_select_db($dbname, $con);

$value1 = mysql_real_escape_string($input_string);

$query = 'SELECT * FROM table1 WHERE table1.col1=' . $value1 . '';
$result = mysql_query($query, $con);

while($row = mysql_fetch_assoc*$result)
{
    $col1 = $row['col1'];
    $col2 = $row['col2'];

    echo $col1 . ' ' . $col2 . '<br />';
}

mysql_close($con);
?>

推荐答案

注意:mysql_ 转换为 mysqli_ 可能不是最佳的.如果您准备将所有代码转换为 面向对象.

尝试用 mysqli_ 替换 mysql_ 的所有实例并祈祷它起作用是很诱人的.你会很接近但不完全正确.

Note: Converting from mysql_ to mysqli_ may not be optimal. Consider PDO if you're prepared to convert all of your code to OOP.

It can be tempting to try to replace all instances of mysql_ with mysqli_ and pray it works. You'd be close but not quite on point.

幸运的是,mysqli_connect 的工作非常接近mysql_query 你可以换掉它们的函数名.

Fortunately, mysqli_connect works closely enough to mysql_query that you can just swap out their function names.

mysql_:

$con = mysql_connect($host, $username, $password);

mysqli_:

$con = mysqli_connect($host, $username, $password);

选择数据库

现在,对于mysqli_ 库中的大多数其他函数,您需要将mysqli_select_db 数据库连接作为其第一范围.大多数mysqli_ 函数首先需要连接对象.

Selecting a database

Now, with most of the other functions in the mysqli_ library, you'll need to pass mysqli_select_db the database connection as its first parameter. Most of the mysqli_ functions require the connection object first.

对于这个函数,你可以切换传递给函数的参数的顺序.如果您之前没有向它传递连接对象,现在必须将其添加为第一个参数.

For this function, you can just switch the order of the arguments you pass to the function. If you didn't pass it a connection object before, you have to add it as the first parameter now.

mysql_:

mysql_select_db($dbname, $con);

mysqli_:

mysqli_select_db($con, $dbname);

作为奖励,您还可以将数据库名称作为第四个参数传递给 mysqli_connect - 绕过调用 mysqli_select_db 的需要.

As a bonus, you can also pass the database name as the fourth parameter to mysqli_connect - bypassing the need to call mysqli_select_db.

$con = mysqli_connect($host, $username, $password, $dbname);

清理用户输入

使用mysqli_real_escape_stringmysql_real_escape_string 非常相似.您只需要将连接对象作为第一个参数传递.

Sanitize user input

Using mysqli_real_escape_string is very similar to mysql_real_escape_string. You just need to pass the connection object as the first parameter.

mysql_:

$value1 = mysql_real_escape_string($input_string);

mysqli_:

$value1 = mysqli_real_escape_string($con, $input_string);

非常重要:准备和运行查询

mysql_ 函数开始被弃用的一个原因是它们无法处理准备好的语句.如果您只是将代码转换为 mysqli_ 而没有采取这一重要步骤,那么您将受到 mysql_ 函数的一些最大弱点的影响.

Very Important: Preparing and Running a Query

One reason the mysql_ functions were deprecated to begin with was their inability to handle prepared statements. If you simply convert your code to mysqli_ without taking this important step, you are subject to some of the largest weaknesses of the mysql_ functions.

值得阅读这些关于准备好的语句及其好处的文章:

It's worth reading these articles on prepared statements and their benefits:

维基百科 - 准备好的声明

PHP.net - MySQLi 准备好的语句

注意:使用准备好的语句时,最好明确列出您尝试查询的每一列,而不是使用 * 符号来查询所有列.通过这种方式,您可以确保在对 mysqli_stmt_bind_result 的调用中考虑了所有列.

Note: When using prepared statements, it's best to explicitly list each column you're attempting to query, rather than using the * notation to query all columns. This way you can ensure you've accounted for all of the columns in your call to mysqli_stmt_bind_result.

mysql_:

$query = 'SELECT * FROM table1 WHERE table1.col1=' . $value1 . '';
$result = mysql_query($query, $con);
while($row = mysql_fetch_assoc*$result)
{
    $col1 = $row['col1'];
    $col2 = $row['col2'];

    echo $col1 . ' ' . $col2 . '<br />';
}

mysqli_:

$query = 'SELECT col1,col2 FROM table1 WHERE table1.col1=?';
if ($stmt = mysqli_prepare($link, $query)) {

    /* pass parameters to query */
    mysqli_stmt_bind_param($stmt, "s", $value1);

    /* run the query on the database */
    mysqli_stmt_execute($stmt);

    /* assign variable for each column to store results in */
    mysqli_stmt_bind_result($stmt, $col1, $col2);

    /* fetch values */
    while (mysqli_stmt_fetch($stmt)) {
        /*
            on each fetch, the values for each column 
            in the results are automatically stored in 
            the variables we assigned using 
            "mysqli_stmt_bind_result"
        */
        echo $col1 . ' ' . $col2 . '<br />';
    }

    /* close statement */
    mysqli_stmt_close($stmt);
}

显示错误

显示错误的方式与 mysqli_ 略有不同.mysqli_error 需要连接对象作为其第一个参数.但是如果连接失败怎么办?mysqli_ 引入了一小组不需要连接对象的函数:mysqli_connect_* 函数.

Showing errors

Showing errors works a little differently with mysqli_. mysqli_error requires the connection object as its first parameter. But what if the connection failed? mysqli_ introduces a small set of functions that don't require the connection object: the mysqli_connect_* functions.

mysql_:

if (!$con) {
    die('Could not connect: ' . mysql_error());
}

if (!$result) {
    die('SQL Error: ' . mysql_error());
}

mysqli_:

/* check connection error*/
if (mysqli_connect_errno()) {
    die( 'Could not connect: ' . mysqli_connect_error() );
}

/* check query error */
if ($stmt = mysqli_prepare($link, $query)) {

    // ... execute query

    if (mysqli_stmt_error($stmt)) {
        echo 'SQL Error: ' . mysqli_stmt_error($stmt);
    }
}

这篇关于如何将使用 mysql_ 函数的脚本转换为使用 mysqli_ 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:mysqli::multi_query 是否比多个单个查询更有效? 下一篇:新的 Mysqli 对象为空

相关文章