如何将mysql更改为mysqli? [英] How to change mysql to mysqli?

查看:37
本文介绍了如何将mysql更改为mysqli?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于下面这段代码,我用于常规 mysql,如何将其转换为使用 mysqli?

Based on this code below I use for regular mysql, how could I convert it to use mysqli?

是不是把mysql_query($sql);改成mysqli_query($sql);这么简单?

Is it as simple as changing mysql_query($sql); to mysqli_query($sql);?

<?PHP

//in my header file that is included on every page I have this
$DB["dbName"] = "emails";
$DB["host"] = "localhost";
$DB["user"] = "root";
$DB["pass"] = "";
$link = mysql_connect($DB['host'], $DB['user'], $DB['pass']) or die("<center>An Internal Error has Occured. Please report following error to the webmaster.<br><br>".mysql_error()."'</center>");
mysql_select_db($DB['dbName']);
// end header connection part

// function from a functions file that I run a mysql query through in any page.
function executeQuery($sql) {
    $result = mysql_query($sql);
    if (mysql_error()) {
        $error = '<BR><center><font size="+1" face="arial" color="red">An Internal Error has Occured.<BR> The error has been recorded for review</font></center><br>';
        if ($_SESSION['auto_id'] == 1) {
            $sql_formatted = highlight_string(stripslashes($sql), true);
            $error .= '<b>The MySQL Syntax Used</b><br>' . $sql_formatted . '<br><br><b>The MySQL Error Returned</b><br>' . mysql_error();
        }
        die($error);
    }
    return $result;
}

// example query ran on anypage of the site using executeQuery function
$sql='SELECT auto_id FROM friend_reg_user WHERE auto_id=' .$info['auto_id'];
$result_member=executequery($sql);
if($line_member=mysql_fetch_array($result_member)){
    extract($line_member);
} else {
    header("location: index.php");
    exit;
}
?>

推荐答案

首先要做的可能是将每个 mysql_* 函数调用替换为其等效的 mysqli_*>,至少如果您愿意使用过程式 API——考虑到您已经有一些基于 MySQL API 的代码,这是一种更简单的方法,这是一个过程式的.

The first thing to do would probably be to replace every mysql_* function call with its equivalent mysqli_*, at least if you are willing to use the procedural API -- which would be the easier way, considering you already have some code based on the MySQL API, which is a procedural one.

为了帮助解决这个问题,MySQLi 扩展功能摘要绝对是有帮助的东西.

To help with that, the MySQLi Extension Function Summary is definitely something that will prove helpful.

例如:

  • mysql_connect will be replaced by mysqli_connect
  • mysql_error will be replaced by mysqli_error and/or mysqli_connect_error, depending on the context
  • mysql_query will be replaced by mysqli_query
  • and so on

注意:对于一些函数,你可能需要仔细检查参数:也许这里和那里有一些差异——但不是那么多,我想说:mysql和mysqli都是基于相同的库 (libmysql ; 至少对于 PHP <= 5.2)

例如:

  • 对于 mysql,你必须使用 mysql_select_db 连接后,指示您要在哪个数据库上进行查询
  • 另一方面,mysqli 允许您将该数据库名称指定为 mysqli_connect.
  • 还有一个mysqli_select_db 您可以使用的功能(如果您愿意).

这篇关于如何将mysql更改为mysqli?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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