php 从 MYSQL 更新到 MYSQLI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12020227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating from MYSQL to MYSQLI
提问by dave
So as MYSQL is deprecated and eveyone keeps telling me to update, I thought it was about time I did.
因此,由于 MYSQL 已被弃用,并且每个人都不断告诉我要更新,我认为是时候这样做了。
But as I'm not used to mysqli_*, it seems alien to me. And it's not a simple edit when I have a whole site coded in Mysql.
但由于我不习惯 mysqli_*,这对我来说似乎很陌生。当我用 Mysql 编码整个站点时,这不是一个简单的编辑。
So I'm wondering: How would I transform the below code into Mysqli? Just to give me and anyone else a good starting point when dealing with querying the database.
所以我想知道:我如何将下面的代码转换成 Mysqli?只是为了在处理查询数据库时为我和其他任何人提供一个良好的起点。
$sql_follows="SELECT * FROM friends WHERE user1_id=".$_SESSION['id']." AND status=2 OR user2_id=".$_SESSION['id']." AND status=2";
$query_follows=mysql_query($sql_follows) or die("Error finding friendships");
if($query_follows>0){
}
EDIT:On reading up and editing my whole site the above code converted to MYSQLI_ would go something like this..
编辑:在阅读和编辑我的整个网站时,上面的代码转换为 MYSQLI_ 会变成这样..
$Your_SQL_query_variable= mysqli_query($connectionvariable,"SELECT * FROM friends WHERE user1_id=".$_SESSION['id']." AND status=2 OR user2_id=".$_SESSION['id']." AND status=2")) {
printf("Error: %s\n", $mysqli->error);
}
回答by Barmar
You can download a converter tool from here:
您可以从这里下载转换器工具:
https://github.com/philip/MySQLConverterTool
https://github.com/philip/MySQLConverterTool
The code it generates is pretty gross, mainly because of the way it implements the default database link argument with a $GLOBALvariable. (This also makes it easy to recognize when someone is using code that's gone through the converter.)
它生成的代码非常粗糙,主要是因为它使用$GLOBAL变量实现默认数据库链接参数的方式。(这也便于识别何时有人使用通过转换器的代码。)
There's also a MySQL Shim Library located here:
这里还有一个 MySQL Shim 库:
回答by Conrad Lotz
The best place to transform is to look at the php reference library. Very easy to use and will tell you everything you need to know:
转换最好的地方是看php参考库。非常易于使用,并会告诉您您需要知道的一切:

