Connecting to MySQL and Selecting the Database

The first step when dealing with MySQL—connecting to the server—requires the appropriately named mysql_connect() function:

$dbc = mysql_connect ($host, $user,
→ $password);

The arguments sent to the function (host, username, and password) are based upon the users and privileges set up within the mysql database (see Appendix A, “Installation,” for more information). Commonly (but not always), the host to specify will be localhost (naming a different host will allow you to connect to MySQL running on a different server).

If the connection was made, the $dbc variable will become a reference point for all of your subsequent database interactions. Most of the PHP functions for working with MySQL can ...

Get PHP and MySQL for Dynamic Web Sites: Visual Quickpro Guide, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.