Connecting to a MySQL database

resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]])

bool mysql_select_db ( [string database_name [, resource link_identifier]])

To connect to MySQL, there are two key functions: mysql_connect() and mysql_select_db(). Mysql_connect() usually takes three arguments - the IP address of a MySQL server to connect to, the username you wish to logon as, and the password for that username, and opens a database connection for your PHP script - it lets you execute commands and read results from any database which allows you access. Do not worry about disconnecting from your database - PHP automatically disconnects you when it finishes running your script. Note that examples in this book will always use the username "phpuser" and the password "alm65z" - do not use them in your own scripts, for security reasons!

All SQL queries you run in PHP, unless you specifically say otherwise (which we shall not be doing) will be executed on the most recent connection you open in your script. Each script needs to open its own database connection through which to execute its database queries, although, by using a persistent connection, they can be made to share connections

The first parameter to mysql_connect() can either by an IP address, or a hostname. Most operating systems also allow you to use "localhost" as the local computer and have MySQL connect directly through a local socket. Alternatively, you can specify 127.0.0.1, which is also the local computer, and have MySQL connect through TCP/IP, which is a little slower. To connect to a remote server, just enter either the hostname (e.g. www.microsoft.com) or the IP address (e.g. 212.113.192.101) as the first parameter, and your data will be transparently sent over the Internet.

Author's Note: In small scenarios, having the PHP server and MySQL server on the same machine makes the most sense, and allows for very high performance. However, in order to be more scalable you will need to have your database server separate from your PHP server, with the two connected via a high-speed network.

Once you have got a connection open, it is best to call mysql_select_db() soon after - it takes just one argument, which is the name of the database you wish to use. Once you select a database, all queries you run are on tables in that database until you select another database, so it is like the USE statement in MySQL. Similarly to mysql_connect(), examples in this book will always use the database "phpdb" - again, you should change this for your own purposes for security reasons.

Like mysql_connect(), you generally only use this function once. Once both are done, you have got a connection to your database and you have got a database selected - you are all set to perform queries.

<?php
    mysql_connect
("localhost", "phpuser", "alm65z");
    
mysql_select_db("phpdb");
?>

Author's Note: Once you are connected, you can use the function mysql_ping() to check whether the server is alive - it automatically uses the most recently opened database connection so you need not pass it any parameters, and it returns true if the server was contacted or false if the connection appears to be lost.

The last two parameters aren't used all that often, but are worth knowing about. Calling mysql_connect() for the first time will open a new connection to the MySQL server, but calling it again in the same script with the same arguments as the first call will just return the previous connection. If you specify parameter four as true (or 1 as is most common), PHP will always open a new connection each time you call mysql_connect().

The last parameter allows you to specify additional connection opens, of which only really useful one is MYSQL_CLIENT_COMPRESS, which tells the server that it may use data compression to save network transfer time - a smart move if your web server and database server are on different machines.

 

Next chapter: Querying and formatting >>

Previous chapter: Using MySQL with PHP

Jump to:

 

Home: Table of Contents

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar