php - 如何将mysql改为mysqli?

标签 php mysql mysqli

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

是否就像将 mysql_query($sql); 更改为 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 MySQLi Extension Function Summary绝对是有帮助的。

例如:

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

例如:

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

完成后,尝试执行脚本的新版本...并检查一切是否正常;如果没有...是时候寻找错误了;-)

关于php - 如何将mysql改为mysqli?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31500029/

相关文章:

php - API 平台中的自定义操作不会 Autowiring 实体

mysql - 如何在mysql中对非常大的消息表进行分区

mysql - 理解模逻辑

javascript - 表单在 php while 循环中呈现相同的数据

php - 在PHP中过滤MySQL数据以仅显示当前 session 用户的数据?

php - 在没有 Adob​​e Reader 的情况下将 pdf 文件显示到浏览器中

php - 如何将非验证手机号码用户重定向到验证页面

php - 模板内的 Joomla 自定义字段

php - 向表中插入多行想了解发生或未发生的过程

php - 如何计算mysql中符合条件的列数?