Difference between mysql_connect() and mysqli_connect() functions in PHP

Asked

Viewed 15,034 times

12

I wanted to know the difference between the two and which is the best one to use.

$x= mysqli_connect("localhost","my_user","my_password","my_db");
$y= mysql_connect("localhost", "my_user", "my_password");

Are some of them better or newer than the other? If it is more recent, what is the advantage of using the X function instead of the Y function?

4 answers

9


In addition to the points cited in the other answers about the advantages of mysqli:

  • Object Oriented Interface (Object-oriented interface)

  • Support for Prepared Statements

  • Support for multiples Statements

  • Support for transactions (Transactions)

  • Improvement in debugging capacity

  • Support for embedded server

It is also necessary to note that the functions mysql_ no longer receive updates such as fixes and improvements and this is the vital point for you no longer to use the mysql_, because in the future it will soon cease to exist for the new versions of PHP.

In other words, if you continue to function mysql_, two situations can happen with your projects:

  1. There may be gaps in API security mysql_ or bugs.
  2. When the API mysql_ if disabled, your scripts will stop working, which will cause you a lot of headache as you will have to redo several codes.

Note that mysqli means Mysql Improved in Portuguese, it would be something like Improved Mysql, referencing the API and not the database system.

8

This is the latest and most recommended, the mysql_connect is discontinued, with the mysqli_connect the newest and uses object orientation, Prepared statements, supports multiple statements, transaction support has better convenience to debug:

mysqli_connect("localhost","my_user","my_password","my_db");

7

5

The functions of the Mysqli library are newer and more complex, allowing you to execute stored procedures, functions and other commands that with the Mysql library were not possible.

Prefer to use the functions mysqli_*

Browser other questions tagged

You are not signed in. Login or sign up in order to post.