What is difference between mysql_connect and mysqli_connect?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The mysqli_connect() function in PHP is used to connect you to the database. In the previous version of the connection mysql_connect() was used for connection and then there comes mysqli_connect() where i means improved version of connection and is more secure than mysql_connect().
    Syntax: 
     

    mysqli_connect ( "host", "username", "password", "database_name" )

    Parameters used: 
     

    • host: It is optional and it specify the host name or IP address. In case of local server localhost is used as a general keyword to connect local server and run the program.
    • username: It is optional and it specify mysql username. In local server username is root.
    • Password: It is optional and it specify mysql password.
    • database_name: It is database name where operation perform on data. It also optional.

    Return values: 
     

    • It returns an object which represent MySql connection. If connection failed then it return FALSE.

    Program: Below is the implementation of mysqli_connect() function.
     

    php

        mysqli_connect("localhost", "root", "", "GFG");

        if(mysqli_connect_error())

            echo "Connection Error.";

        else

            echo "Database Connection Successfully.";

    ?>

    Output: 
     

    Database Connection Successfully.

    Hello,

    mysqli_pconnect() function is used for making a persistence connection with the database that does not terminate when the script ends.

    mysqli_connect() function searches any existing persistence connection first and if no persistence connection exists, then it will create a new database connection and terminate the connection at the end of the script.

    Sample code:

    $DBconnection = mysqli_connect("localhost","username","password","dbname");
    
    // Check for valid connection
    
    if (mysqli_connect_errno())
    
    {
    
    echo "Unable to connect with MySQL: " . mysqli_connect_error();
    
    }

    mysqli_pconnect() function is depreciated in the new version of PHP, but you can create persistence connection using mysqli_connect with the prefix p.

    Thank You!!

    What is the difference between mysqli_connect and mysql_connect

    Questions : What is the difference between mysqli_connect and mysql_connect

    2022-09-17T05:56:59+00:00 2022-09-17T05:56:59+00:00

    866

    What is the difference between anycodings_mysql mysqli_connect and mysql_connect?

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

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

    Total Answers 6

    28

    Answers 1 : of What is the difference between mysqli_connect and mysql_connect

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

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

    0

    2022-09-17T05:56:59+00:00 2022-09-17T05:56:59+00:00Answer Link

    mRahman

    1

    Answers 2 : of What is the difference between mysqli_connect and mysql_connect

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

    They are not interchangeable.

    0

    2022-09-17T05:56:59+00:00 2022-09-17T05:56:59+00:00Answer Link

    miraj

    5

    Answers 3 : of What is the difference between mysqli_connect and mysql_connect

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

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

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

    There are several important differences anycodings_php 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

    0

    2022-09-17T05:56:59+00:00 2022-09-17T05:56:59+00:00Answer Link

    miraj

    5

    Answers 4 : of What is the difference between mysqli_connect and mysql_connect

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

    MySQLi provides a object-oriented way anycodings_php for accessing MySQL databases.

    in short: if you use mysql_query(), you anycodings_php should use mysql_connect() to connect to anycodings_php your server.

    Others already postet links to the PHP anycodings_php manual.

    0

    2022-09-17T05:56:59+00:00 2022-09-17T05:56:59+00:00Answer Link

    jidam

    1

    Answers 5 : of What is the difference between mysqli_connect and mysql_connect

    Mysqli_connect is the newer version of anycodings_php mysql library.

    Here I in mysqli stands for improved.

    Few things have been introduced with anycodings_php Mysqli.

    They are,

    -Prepared statements.

    -Object oriented interface.

    -Support for multiple statements.

    -Embedded server support.

    The mysqli extension has a number of anycodings_php benefits, the key enhancements over the anycodings_php 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 anycodings_php database connector to Mysql whereas anycodings_php Mysqli_connect() is the connector for anycodings_php Mysqli databse

    0

    2022-09-17T05:56:59+00:00 2022-09-17T05:56:59+00:00Answer Link

    miraj

    1

    Answers 6 : of What is the difference between mysqli_connect and mysql_connect

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

    0

    2022-09-17T05:56:59+00:00 2022-09-17T05:56:59+00:00Answer Link

    raja

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

    (1) mysql_connect(host name ,username,password); and mysqli_connect(host name ,username,password,db) is that the mysql take 3 parameter and mysqli take 4 parameter to connect the database server .

    What is mysqli_connect?

    The mysqli_connect() function establishes a connection with MySQL server and returns the connection as an object.

    What is the difference between New mysqli and mysqli_connect?

    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) .

    What does mysql_connect return?

    The mysql_connect() function opens a non-persistent MySQL connection. This function returns the connection on success, or FALSE and an error on failure.