37
The short answer is no, the functions are not equivalent.
简短的回答是否定的,功能不相同。
The good news is there is a converter tool that will help you if you've got a lot of calls/projects to change. This will allow your scripts to work right away.
好消息是有一个转换器工具可以帮助你,如果你有很多要改变的电话/项目。这将允许您的脚本立即工作。
https://github.com/philip/MySQLConverterTool
https://github.com/philip/MySQLConverterTool
It's a forked version of the Oracle original version, and it's kosher.
它是Oracle原始版本的分叉版本,它是犹太洁食。
That said, it's not too difficult to update your code, and you might want to migrate to an object orientated methodology anyway ...
也就是说,更新代码并不太难,而且您可能希望迁移到面向对象的方法......
1) The Connection
1)连接
For all intents and purposes, you need a new connection function that saves the connection as a PHP variable, for example;
出于所有意图和目的,您需要一个新的连接函数,例如将连接保存为PHP变量;
$mysqli = new mysqli($host,$username,$password,$database);
Notice I've saved the connection to $mysqli
. You can save to $db
or whatever you like, but you should use this throughout your code to reference the connection.
注意我已经保存了与$ mysqli的连接。您可以保存到$ db或任何您喜欢的内容,但是您应该在整个代码中使用它来引用连接。
Remember to check for a connection error though;
请记住检查连接错误;
if ($mysqli->connect_errno) echo "Error - Failed to connect to MySQL: " . $mysqli->connect_error;
2) The Query
2)查询
Note: You should protect against SQL injection with prepared statements, which are available in MySQLi. Take a look at How can I prevent SQL injection in PHP?, but I'm just going to cover the basics here.
注意:您应该使用MySQLi中提供的预准备语句来防止SQL注入。看一下如何在PHP中阻止SQL注入?但是我将在这里介绍基础知识。
You now have to include the connection as an argument in your query, and other mysqli_
functions. In procedural code it's the first argument, in OO you write it like a class method;
您现在必须在查询中包含连接作为参数,以及其他mysqli_函数。在程序代码中它是第一个参数,在OO中你把它写成类方法;
Procedural:
程序:
$result = mysqli_query($mysqli,$sql);
OO:
OO:
$result = $mysqli->query($sql);
3) Fetch Result
3)获取结果
The fetching of the result is similar to the old mysql_
function in procedural;
获取结果类似于过程中的旧mysql_函数;
while($row = mysqli_fetch_assoc($result))
but as $result
is now an object in mysqli, you can use the object function call;
但是由于$ result现在是mysqli中的一个对象,你可以使用对象函数调用;
while($row = $result->fetch_assoc())
4) Close Connection
4)关闭连接
So as before, you need to include the connection in the close function; as an argument in procedural;
和以前一样,你需要在close函数中包含连接;作为程序论证;
mysqli_close($mysqli);
and as the object that you run the function on in OO;
以及在OO中运行函数的对象;
$mysqli->close();
I would be here forever if I went through them all, but you get the idea. Take a look at the documentation for more information. Don't forget to convert any connection close, result release, or error and row counting functions you have.
如果我完全了解它们,我会永远在这里,但你明白了。请查看文档以获取更多信息。不要忘记转换任何连接关闭,结果释放,或错误和行计数功能。
The basic rule of thumb is for functions that use the database connection, you need to include it in the function now (either as the first argument in procedural, or the object you use to call the function in OO), or for a result set you can just change the function to mysqli_
or use the result set as the object.
基本的经验法则是使用数据库连接的函数,您需要将它包含在函数中(作为过程中的第一个参数,或者用于在OO中调用函数的对象),或者用于结果集您可以将函数更改为mysqli_或将结果集用作对象。