Current Location: Home> Function Categories> mysql_connect

mysql_connect

Open a non-persistent MySQL connection.
Name:mysql_connect
Category:Uncategorized
Programming Language:php
One-line Description:Open a connection to the MySQL server

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:

  • $server (optional): MySQL server hostname or IP address. The default value is ini_get("mysql.default_host"), which is the default host name configured in the php.ini file.
  • $username (optional): MySQL login username. The default value is ini_get("mysql.default_user"), which is the default user name configured in the php.ini file.
  • $password (optional): MySQL login password. The default value is ini_get("mysql.default_password"), which is the default password configured in the php.ini file.
  • $new_link (optional): Whether to create a new connection. If set to true, a new connection is created; if set to false, a new connection is attempted to reuse the existing connection. The default value is false.
  • $client_flags (optional): Client flag bit. You can use the MYSQL_CLIENT_* constant to set it. The default value is 0, which means that no specific client flag is used.

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.

Similar Functions
Popular Articles