What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.



mysql_connect — Connects to a MySQL server.


mysql link mysql_connect([host][, user_name][, password]);
string host (optional): Host to connect to, with an optional port or socket component
string user_name (optional): Username to log in with
string password (optional): Password to log in with


MySQL link on success; FALSE on failure


mysql_connect() is used to connect to a local or remote MySQL server. Normally it's used as follows:

   $mysql_link = mysql_connect ('some_host', 'some_user', 'some_password');
You can also let PHP supply optional arguments for all parameters.

The host argument should contain a domain name or an IP address, optionally followed by a port number. If no port number is specified, the default MySQL port of 3306 is assumed. If connecting tolocalhost on a UNIX-like operating system, a path to the local MySQL socket can also be specified, in the form localhost: /path/to/socket - execute a show variables like 'socket' query to find the location of the socket.

If the host argument is not specified, the value of the mysql.default_host configuration directive is used. If this directive is not set, localhost is used. Default values for the port and socket components of the host argument can be specified using the mysql.default_port and mysql.default_socket configuration directives.

The user_name argument should contain the name of the user to authenticate. If user_name is not specified, the value set for the mysql.default_user configuration directive is used. If this directive is not set, the name of the user that PHP is currently running as is used.

The password argument should contain the password to use for the login attempt. If this argument is not specified, the value set for mysql.default_password is used. If this directive is not set, an empty string ("") is used.

If sql.safe_mode is enabled, mysql_connect() and mysql_pconnect() ignore any arguments passed to them. Instead, the functions attempt to connect to a MySQL server on localhost as the user that PHP runs as, with no password.

Duplicate calls to mysql_connect() from within the same script return the same mysql link.

Connections opened by mysql_connect() are closed at script exit, unless closed earlier by calls to mysql_close() .


PHP 3+, PHP 4+

See also

To establish a persistent MySQL connection:


To close a connection opened by mysql_connect() :



Example 790. Connect to the default database

mysql_connect ()
    or die ("Could not connect to a MySQL server using the default settings.");

echo "Successfully connected to a MySQL server using the default settings.";

Example 791. Connect to a remote database on a nonstandard port

$host = '';
$port = '13306';
$user = 'some_login';
$pass = 'some_password';

// Suppress errors with a single ampersand (@)
// Handle failure to connect with a custom error message
$mysql_link = @ mysql_connect ("$host:$port", $user, $pass)
    or die ("Could not connect to a MySQL server at '$host:$port' as user '$user'.");

echo "Connected to a MySQL server at '$host:$port' as user '$user'.";

Example 792. Connect to a local database server using the socket path

$sock = 'localhost:/var/lib/mysql/mysql.sock';
$user   = 'some_login';
$pass   = 'some_password';

// Handle connection errors gracefully
// Redirect the client to an error-handling page
$mysql_link = mysql_connect ($sock, $user, $pass)
    or header ('Location:');

echo "Connected to a MySQL server at $sock as user $user.";

PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at The authors of this book have elected not to choose any options under the OPL. This online book was obtained from and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.