基于下面我用于常规mysql的代码,我如何将其转换为使用Mysqli?
只是将mysql_query($SQL);
更改为mysql_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_*
函数调用替换每个mysql_*
函数调用,至少如果您愿意使用过程性API--考虑到您已经有了一些基于MySQL API的代码,这是一种过程性API,这将是一种更容易的方法。
为了帮助实现这一点,MySQLi扩展函数摘要肯定是很有帮助的。
例如:
mysql_connect
将被mysql_connect
mysql_error
将由mysqli_error
和/或mysqli_connect_error
替换,具体取决于上下文mysql_query
将被mysql_query
注意:对于某些函数,您可能需要仔细检查参数:也许这里那里有一些不同--但不是很多,我要说:mysql和mysqli都基于相同的库(libmysql;至少对于PHP<=5.2)
例如:
mysql_select_db
来指示要在哪个数据库上执行查询mysqli_connect
的第四个参数。mysqli_select_db
函数可以使用。
完成此操作后,请尝试执行新版本的脚本。。。 并检查是否一切正常; 如果不是。。。 寻找虫子的时间;-)
(我意识到这很老了,但它还是会出现。。。)
如果您确实将mysql_*
替换为mysql_*
,请记住,整个mysql_*
函数负载需要传递数据库链接。
例如:
mysql_query($query)
成为
mysqli_query($link, $query)
即需要大量的检查。
我处理这件事最简单的方法
$con = mysqli_connect($serverName,$dbusername,$dbpassword);
3个步骤,按以下顺序更换
这对我每次都管用