HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [237]
$conn = mysql_connect(“localhost”,”user”,”password”) or die(mysql_error());
You’ll need to supply your own username and password. My values here are just samples.
4. Prepare for a graceful crash.
It’s very possible that something will go wrong when you attempt a data connection. The or die() clause tells PHP what to do if something goes wrong while making the connection:
$conn = mysql_connect(“localhost”,”user”,”password”) or die(mysql_error());
How many times in your life have you heard that phrase? “Prepare for a graceful crash.” You’ve got to love programming.
5. Invoke mysql_error() if something goes wrong.
If there’s a problem with the MySQL connection, the error message will come from MySQL, not PHP. To ensure that you see the MySQL error, use the mysql_error() function. If you made a mistake (like misspelling the username), MySQL will report this error to PHP. Use mysql_error() to print out the last error from MySQL:
$conn = mysql_connect(“localhost”,”user”,”password”) or die(mysql_error());
6. Specify the particular database.
After you’re connected to the server, you need to specify which database on the server will be used for your transaction. I’m using a database called xfd for all the examples in this book. The mysql_select_db() function is used to handle this task:
mysql_select_db(“xfd”);
Passing a query to the database
The reason for connecting to a database is to retrieve data from it (or to add or modify data, but the basic approach is always the same). In any case, you need to pass instructions to the database in SQL. (If you’re unfamiliar with SQL, read Book VI, Chapter 1.)
Your PHP program usually constructs an SQL statement in a string variable and then passes this value to the database. For this basic example, I specify the entire query. See the section “Processing the input,” later in this chapter, for some warnings about how to incorporate user information in data queries.
The showContact.php program simply asks for a list of all the values in the contact table of the xfd database. The SQL query for displaying all the data in a table looks like this:
SELECT * FROM contact;
To use an SQL statement, package it into a string variable, like this:
$sql = “SELECT * FROM contact”;
Note that you don’t need to include the semicolon inside the string variable. You can call the variable anything you wish, but it’s commonly called $sql. SQL queries can get complex, so if the SQL requires more than one line, you may want to encase it in a heredoc. (See Chapter 2 of this minibook for information on using heredocs.)
Pass the request to the database using the msql_query() function:
$result = mysql_query($sql, $conn) or die(mysql_error());
The mysql_query() function has a lot going on. Here’s how you put it together:
1. Create a variable to house the results.
When the query is finished, it will send results back to the program. The $result variable will hold that data:
$result = mysql_query($sql, $conn) or die(mysql_error());
2. Invoke mysql_query().
This function passes the query to the database:
$result = mysql_query($sql, $conn) or die(mysql_error());
3. Send the query to the database.
The first parameter is the query. Normally, this is stored in a variable called $sql:
$result = mysql_query($sql, $conn) or die(mysql_error());
4. Specify the connection.
The second parameter is the connection object created when you ran mysql_connect(). If you leave out the connection object, PHP uses the last MySQL connection that was created:
$result = mysql_query($sql, $conn) or die(mysql_error());
5. Handle errors.
If there’s an error in your SQL request, MySQL will send back an error message. Prepare for this with the or die() clause (just like you used for mysql_connect()):
$result = mysql_query($sql, $conn) or die(mysql_error());
6. Return the MySQL error if there was a problem.
If something went wrong in the SQL code, have your program reply with the MySQL error so you’ll at least know what went wrong:
$result