Difference between PHP mySql and mySqli code

By: Prof. Dr. Fazal Rehman | Last updated: February 3, 2024

The basic difference between writing code with PHP MySQL and PHP MySQLi is that MySQLi stands for MySQL improved.

Benefits of MySQLi

  • MySQLi is an improved version of the MySQL extension for writing code in PHP. MySQLi offers an object-oriented API, as well as support for prepared statements and transactions, which are not
  • MySQLi extension uses an object-oriented approach
  • We can use the mysqli_stmt class to create a prepared statement and mysqli_begin_transaction, mysqli_commit, and mysqli_rollback to handle a transaction that is not available in the MySQL extension.
  • MySQLi extension has a more consistent and simplified syntax available in the MySQL extension.
  • MySQLi supports both procedural and object-oriented programming, while the MySQL extension only supports procedural programming.
  • It is recommended to use MySQLi over MySQL as it is more secure and efficient.
Here is an example of connecting to a MySQL database using the MySQL extension in PHP:
<?php $link = mysql_connect(‘hostname’, ‘username’, ‘password’); if (!$link) { die(‘Could not connect: ‘ . mysql_error()); } echo ‘Connected successfully’; mysql_close($link); ?>
And here is an example of connecting to a MySQL database using the MySQLi extension in PHP:
<?php $link = new mysqli(‘hostname’, ‘username’, ‘password’, ‘database_name’); if ($link->connect_error) { die(‘Connect Error (‘ . $link->connect_errno . ‘) ‘ . $link->connect_error); } echo ‘Connected successfully’; $link->close(); ?>
 
All Copyrights Reserved 2025 Reserved by T4Tutorials