new mysqli vs mysqli_connect [closed]

2019-02-05 14:31发布

问题:

What is difference between the new mysqli and mysqli_connect? I know that executing a query is different;
for example: mysqli->query() and mysqli_query()
Why are there two different types, what is the need for the difference?

回答1:

One is for Procedural style programming and other is for OOP style programming. Both serve the same purpose; Open a new connection to the MySQL server

OOP Style usage

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

Procedural Style usage

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

Reference: PHP Manual



回答2:

Right on @Hanky Panky. I'd also add to that the PHP docs:

http://www.php.net/manual/en/mysqli.construct.php

Note:

OO syntax only: If a connection fails an object is still returned. To check if the connection failed then use either the mysqli_connect_error() function or the mysqli->connect_error property as in the preceding examples.

So error handling is just one difference.



标签: php mysqli