PHP mysql_connect() Function

Definition and Usage

The mysql_connect() function opens a non-persistent MySQL connection.

Syntax

mysql_connect(server,user,pwd,newlink,clientflag)
Parameters Description
server

Optional. Specifies the server to connect to.

It can include a port number, such as 'hostname:port', or a path to a local socket, such as ':/path/to/socket' for localhost.

If the PHP directive mysql.default_host is not defined (the default case), the default value is 'localhost:3306'.

user Optional. Username. The default value is the username of the owner of the server process.
pwd Optional. Password. The default value is an empty password.
newlink Optional. If mysql_connect() is called a second time with the same parameters, no new connection will be established; instead, the already opened connection identifier will be returned. The parameter new_link changes this behavior and makes mysql_connect() always open a new connection, even if mysql_connect() has been called with the same parameters before.
clientflag

Optional.clientflags The parameter can be a combination of the following constants:

  • MYSQL_CLIENT_SSL - Uses SSL encryption
  • MYSQL_CLIENT_COMPRESS - Uses a compressed protocol
  • MYSQL_CLIENT_IGNORE_SPACE - Allows spaces after the function name
  • MYSQL_CLIENT_INTERACTIVE - Allows non-active timeout before closing the connection

Return Value

If successful, it returns a MySQL connection identifier, and if it fails, it returns FALSE.

Tips and Comments

Note:When the script ends, the connection to the server is closed unless it has been explicitly called earlier. mysql_close() Closed.

Tip:To create a persistent connection, use mysql_pconnect() Functions.

Example

<?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
// Some code...
mysql_close($con);
?>