基于下面的代码,我用于常规 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_connect
将替换为mysqli_connect
mysql_error
将替换为mysqli_error
和/或mysqli_connect_error
,取决于上下文mysql_query
将替换为mysqli_query
- 等等
注意:对于某些功能,您可能需要仔细检查参数:也许这里和那里存在一些差异 - 但不是那么多,我想说:mysql 和 mysqli 都基于相同的库(libmysql;至少对于 PHP <= 5.2)
例如:
- 对于mysql,你必须使用
mysql_select_db
连接后,指示您要在哪个数据库上执行查询
另一方面,mysqli 允许您将数据库名称指定为 - 不过,还有一个
mysqli_select_db
如果您愿意,您可以使用该功能。
mysqli_connect
的第四个参数。 .
完成后,尝试执行脚本的新版本...并检查一切是否正常;如果没有...是时候寻找错误了;-)
关于php - 如何将mysql改为mysqli?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58046965/