PHP MySQLi Connect

PHP mysqli_connect() function is used to open a new connection to the MySQL server. If the connection is established this function returns the resource, else it returns a null.

Syntax:

mysqli_connect(host,username,password,dbname,port,socket);

Host: This is an optional parameter which is used to specify a host name or an IP address.

Username: This is an optional parameter which is used to specify the MySQL username.

Password: This is an optional parameter which is used to specify the MySQL password.

Dbname: This is an optional parameter which is used to specify the default database to be used.

Port: This is an optional parameter which is used to specify the port number to connect to the MySQL server.

Socket: This is an optional parameter which is used to specify the socket or named pipe to be used.

PHP mysqli_close() function:

PHP mysqli_close() function is used to disconnect a previously opened database connection. It returns true if connection is closed or false. The mysqli_close() function closes

Syntax:

mysqli_close(connection);

Connection: This is a required parameter which is used to specify the MySQL connection to close.

Example:

<!DOCTYPE html>
<html>
<body>
 
<?php
$phpconnect = mysqli_connect("localhost","Ben","Ben_password","Ben_db");
 
if (mysqli_connect_errno())
{
echo "Connection Failed; " . mysqli_connect_error();
}
else
{
echo "Connection Established.<br>";
}
 
mysqli_close($phpconnect);
echo “Connection Closed.”
?> 
 
</body>
</html>

Output:

Connection Established.
Connection Closed.
Content Protection by DMCA.com
Please Share