newmysqli
和 和有什么不一样mysqli_connect
?我知道执行查询是不同的;
比如:mysqli->query()
和mysqli_query()
为什么会有两种不同的类型,有什么区别?
3 回答
一种用于过程式编程,另一种用于 OOP 式编程。两者都有相同的目的;Open a new connection to the MySQL server
OOP 样式用法
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
程序样式的使用
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
参考:PHP手册
就在@Hanky Panky 上。我还要添加 PHP 文档:
http://www.php.net/manual/en/mysqli.construct.php
笔记:
仅面向 OO 语法:如果连接失败,仍会返回一个对象。要检查连接是否失败,请使用 mysqli_connect_error() 函数或 mysqli->connect_error 属性,如前面的示例中所示。
So error handling is just one difference.
I just found a subtle but interesting difference between the two.
If you encounter a connection error with mysqli_connect
(like $connection = mysqli_connect()
), no mysql info will be returned to the $connection variable. As such, you will not be able to identify the error with myqli_errno($connection)
.
However if you encounter a connection error using new mysqli (like $connection = new mysqli()
), the mysql info WILL be returned and you can check the error with $connection->connect_errno
.
Knowing this, I'd opt for new mysqli.
Oops... Just saw answer from Rick Buczynski and realized after posting that I'm restating what he said, but his reply is more informative.