Open In App

PHP mysqli_connect() Function

Last Updated : 12 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The mysqli_connect() function in PHP is a fundamental tool for establishing a connection to a MySQL database. This function is crucial for PHP applications that need to interact with MySQL databases, enabling them to execute queries, retrieve data, and perform various database operations.

In this article, We will learn about the PHP mysqli_connect() Function in detail by understanding various examples and so on.

PHP mysqli_connect() Function

  • The mysqli_connect() function in PHP is used to establish a connection to a MySQL database. This function initiates a connection to the MySQL server and returns a connection object on success, or FALSE failure.
  • It allows PHP scripts to interact with a MySQL database to perform various operations like querying, inserting, updating, and deleting data.

Syntax: 

mysqli_connect ( "host", "username", "password", "database_name" );
  • host: The hostname or IP address of the MySQL server.
  • username: The username to access the MySQL server.
  • password: The password associated with the given username.
  • database (optional): The name of the database to select upon connection.
  • port (optional): The port number to connect to the MySQL server. Default is 3306.
  • socket (optional): The socket or named pipe to use for the connection.

Parameter Values

  • host: A string representing the MySQL server’s address.
  • username: A string representing the username for MySQL authentication.
  • password: A string representing the password for MySQL authentication.
  • database: A string representing the database name to use (optional).
  • port: An integer representing the port number (optional).
  • socket: A string representing the socket or pipe for the connection (optional).

Return Value:

  • It returns an object which represents the MySql connection. If the connection failed then it returns FALSE.

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

php
<?php
mysqli_connect("localhost", "root", "", "GFG");

if (mysqli_connect_error()) {
    echo "Connection Error.";
} else {
    echo "Database Connection Successfully.";
}
?>

Output:

Database Connection Successfully.

We can also use the die() method to terminate the program with a message if the connection fails.

Program: Below is the implementation of the die() method with the mysqli_connect() method.

PHP
<?php
mysqli_connect("localhost", "root", "", "GFG") or
    die("Connection Unsuccessfull");

// If the connection is successfull then 
// the program does not terminates
// and the following message is displayed.
echo "Connection Successfull";
?>

Output:

Database Connection Successfully.

Conclusion

The mysqli_connect() function is essential for establishing a connection between PHP scripts and a MySQL database. By correctly configuring its parameters, developers can securely and efficiently interact with MySQL databases, leveraging the power of MySQLi for a wide range of database operations.



Next Article

Similar Reads