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?
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) .
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.
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.
The mysqli_connect() function establishes a connection with MySQL server and returns the connection as an object.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With