What is the difference between mysql_connect() and mysqli_connect()

MySQL extension and MySQLi extension are both used to connect to MySQL databases in PHP. However, there are significant differences between mysql_connect() and mysqli_connect().

mysql_connect():

  • Deprecated as of PHP 5.5.0 and removed in PHP 7.0.0.
  • Only supports procedural programming.
  • Does not support prepared statements, which can help protect against SQL injection.
  • More basic and limited feature set compared to MySQLi.

mysqli_connect():

  • Supports both procedural and object-oriented programming styles.
  • Includes support for prepared statements, which enhances security against SQL injections.
  • Supports transactions, multiple statements, and a more robust set of features.

Given that mysql_connect() is deprecated, it is recommended to use mysqli_connect() for new applications.

<?php // Example of using mysqli_connect $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>

mysql_connect mysqli_connect PHP database connections secure database connection PHP database