31

What is the difference between mysqli_connect and mysql_connect?

I'm just wondering when I should use which one. I see both being used, and it seems like they are interchangable.

Which connection is better and how are they fundamentally different when connecting?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
locoboy
  • 38,002
  • 70
  • 184
  • 260
  • [Mysqli_connect Vs Mysql_connect?](http://answers.oreilly.com/topic/2557-mysqli-connect-vs-mysql-connect/) – Joe Stefanelli Jun 17 '11 at 18:28
  • mysqli_* functions are used with a mysqli_connect resource and mysql_* functions are used with a mysql_connect resource. mysqli has more features and is the more current version to use. you may also want to look into PDO which is an OO way of connecting to databases. – dqhendricks Jun 17 '11 at 18:38
  • @dqhendricks: technically that mysqli "resource" is an object, which makes mysqli OO and procedural styles fully interchangeable... but completely incompatible with all old mysql extension functions. – bob-the-destroyer Jun 17 '11 at 20:01
  • @bob-the-destroyer good point. i guess the advantage of PDO is that it gives you a common interface for interacting with a variety of rdbms – dqhendricks Jun 17 '11 at 21:00
  • See also: [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189) – Simba Nov 03 '15 at 13:30
  • The title is deceiving, you are asking about mysql_connect/mysqli_connect but you are really talking (and all replies too) about the differences between the mysql and mysqli drivers in general. Differences in the functions (return values, paramenters, etc..) are apparently not minimal and should be the matter of this question, according to your title. – FrancescoMM Mar 22 '17 at 10:44

6 Answers6

13

They're not interchangeable. There are different extensions to access MySQL databases.

See http://ca2.php.net/manual/en/book.mysqli.php and http://ca2.php.net/manual/en/book.mysql.php.

Francois Deschenes
  • 24,816
  • 4
  • 64
  • 61
  • So are they interchangeable with v4.1.3 and newer? – locoboy Jun 17 '11 at 18:32
  • @cfarm54 - Both can be used. They're no interchangeable (as in you connect with `mysql_connect()` and then try using `mysqli_query()`). The MySQLi overview page says: "If you are using MySQL versions 4.1.3 or later it is strongly recommended that you use the mysqli extension instead." – Francois Deschenes Jun 17 '11 at 18:35
13

mysqli_*() is the modern way to access a MySQL database via PHP.

They are not interchangeable.

alex
  • 479,566
  • 201
  • 878
  • 984
12

Check out the documentation: http://ca3.php.net/manual/en/mysqli.overview.php

From the section "What is PHP's mysqli Extension?"

The mysqli extension, or as it is sometimes known, the MySQL improved extension, was developed to take advantage of new features found in MySQL systems versions 4.1.3 and newer. The mysqli extension is included with PHP versions 5 and later.

There are several important differences between the two libraries:

  • Mysqli supports charsets, mysql does not
  • Mysqli supports prepared statements, mysql does not
  • Mysql does not support multiple statements, mysqli does
Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
Chris Baker
  • 49,926
  • 12
  • 96
  • 115
3

MySQL and MySQLi are two separate PHP extensions, MySQLi being the newer one. Although the connect functions may be interchangeable, I would disadvise to do so!

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.

fholzer
  • 340
  • 3
  • 13
  • 1
    Nothing between mysql and mysqli extensions is interchangeable. Old mysql functions can only work on mysql _resources_. Mysql_i_ functions and methods can only work on mysqli _objects_. Mysql_i_ procedural and OO styles are however fully interchangeable. Basically, when you execute a mysql_i_ procedural function, all you're doing is wrapping the mysqli object and executing its internal method. – bob-the-destroyer Jun 17 '11 at 19:45
2

Mysqli_connect is the newer version of mysql library.

Here I in mysqli stands for improved.

Few things have been introduced with Mysqli.

They are,

-Prepared statements.

-Object oriented interface.

-Support for multiple statements.

-Embedded server support.

The mysqli extension has a number of benefits, the key enhancements over the mysql extension being:

-Object-oriented interface

-Support for Prepared Statements

-Support for Multiple Statements

-Support for Transactions

-Enhanced debugging capabilities

-Embedded server support

So Mysql_connect() basically is the database connector to Mysql whereas Mysqli_connect() is the connector for Mysqli databse

Ganesh Hargude
  • 377
  • 3
  • 2
0

There are too many differences between these MYSQL and MYSQLi PHP database extensions. These differences are based upon some factors like features, performance, benefits, library functions, security and others. The “i” in mysqli stands for “improved”. The “mysqli” extension is an improvement over the old “mysql” extension.Click here for more information

enter image description here

Umang Soni
  • 521
  • 5
  • 9