mysql_connect() Function in PHP

What is mysql_connect function in PHP?
How does mysql_connect() works?

Explanation

The mysql_connect() function Open a connection to a MySQL Server.
Syntax
resource mysql_connect ( [string server [, string username
[, string password [, bool new_link [, int client_flags]]]]])

Returns a MySQL link identifier on success, or FALSE on failure.
Connection to Mysql database can be established by using mysql_connect function. This function takes three parameters, first one is hostname then user-id and then password. We can give the port number along with the hostname also.

$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")

The above function will return resource id or false depending on the success of the connection.
Mysql_connect once establish the connection the link will be present till the script execution is over. It will close itself once the script execution is over or the function mysql_close() is called.
Example:

<?php
// Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
print 'You are connected';
mysql_close($link) ;
?>

See also: mysql_pconnect() and mysql_close()

PHP Topics


Ask Questions

Ask Question