Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new mysqli vs mysqli_connect

Tags:

php

mysqli

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?

like image 205
FosAvance Avatar asked Mar 29 '13 16:03

FosAvance


People also ask

What is the difference between New Mysqli and mysqli_connect?

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) .

What is the difference between Mysql_connect () and mysqli_connect ()?

MySQLi provides a object-oriented way for accessing MySQL databases. in short: if you use mysql_query(), you should use mysql_connect() to connect to your server. Others already postet links to the PHP manual. Nothing between mysql and mysqli extensions is interchangeable.

What is new Mysqli in PHP?

PHP offers two different ways to connect to MySQL server: MySQLi (Improved MySQL) and PDO (PHP Data Objects) extensions. While the PDO extension is more portable and supports more than twelve different databases, MySQLi extension as the name suggests supports MySQL database only.

What is mysqli_connect?

The mysqli_connect() function establishes a connection with MySQL server and returns the connection as an object.


2 Answers

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

like image 111
Hanky Panky Avatar answered Sep 19 '22 21:09

Hanky Panky


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.

like image 38
Rick Buczynski Avatar answered Sep 18 '22 21:09

Rick Buczynski