Function name: mysql_connect()
Applicable version: PHP 5.x - PHP 7.0.x
Usage: The mysql_connect() function is used to open a connection to the MySQL server.
Syntax: resource mysql_connect( [string $server = ini_get("mysql.default_host")], [string $username = ini_get("mysql.default_user")], [string $password = ini_get("mysql.default_password")], [bool $new_link = false], [int $client_flags = 0] )
Parameter description:
Return value: If the connection is successful, a MySQL connection identifier (resource) is returned, which can be used in other MySQL-related functions. If the connection fails, false is returned.
Example: <?php $host = 'localhost'; $username = 'root'; $password = '123456'; $db = mysql_connect($host, $username, $password); if (!$db) { die('Connection failed:' . mysql_error()); }
echo 'The connection was successful! '; mysql_close($db); ?> The above example demonstrates how to use the mysql_connect() function to connect to a MySQL server. First, we provide the host name, user name, and password of the MySQL server, and then use these parameters to call the mysql_connect() function. If the connection is successful, a valid connection identifier ($db) is returned, which we can use to perform other MySQL operations. Finally, we use the mysql_close() function to close the connection.
Note that the mysql_connect() function is discarded in PHP 5.5.0 and removed in PHP 7.0.0. It is recommended to use mysqli or PDO extensions instead of mysql extensions.