问题描述
基于以下我用于常规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;
}
?>
推荐答案
第一件事可能是用等效的mysqli_*
替换每个mysql_*
函数调用,至少在您愿意使用该过程的情况下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 The MySQLi Extension Function Summary is definitely something that will prove helpful.
例如:
-
mysql_connect
将替换为mysqli_connect
-
mysql_error
将替换为mysqli_error
/ormysqli_connect_error
,具体取决于上下文 -
mysql_query
将替换为mysqli_query
- 依此类推
mysql_connect
will be replaced bymysqli_connect
mysql_error
will be replaced bymysqli_error
and/ormysqli_connect_error
, depending on the contextmysql_query
will be replaced bymysqli_query
- and so on
注意:对于某些功能,您可能需要仔细检查参数:也许在这里和那里有一些区别-但不是很多,我要说:mysql和mysqli都是基于相同的库(libmysql;至少对于PHP< = 5.2)
Note: For some functions, you may need to check the parameters carefully: Maybe there are some differences here and there -- but not that many, I'd say: both mysql and mysqli are based on the same library (libmysql ; at least for PHP <= 5.2)
例如:
- 对于mysql,您必须使用
mysql_select_db
连接后,即可指示您要在哪个数据库上进行查询 另一方面, - mysqli允许您将数据库名称指定为
mysqli_connect
. - 仍然,还有一个
mysqli_select_db
您可以根据需要使用的功能.
- with mysql, you have to use the
mysql_select_db
once connected, to indicate on which database you want to do your queries - mysqli, on the other side, allows you to specify that database name as the fourth parameter to
mysqli_connect
. - Still, there is also a
mysqli_select_db
function that you can use, if you prefer.
完成此操作后,尝试执行脚本的新版本...并检查是否一切正常;如果没有的话……是时候寻找bug了;-)
Once you are done with that, try to execute the new version of your script... And check if everything works ; if not... Time for bug hunting ;-)
这篇关于如何将mysql更改为mysqli?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!