mysql_connectDescriptionmysql_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() . See also
ExampleExample 790. Connect to the default database <?php 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 <?php $host = 'mysql.example.com'; $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 <?php $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: http://www.example.com/mysql_connect_error.php'); 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
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
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.
|