Microsoft Word - Sam's Teach Yourself MySQL in 21 Days - SAMS.doc

(singke) #1
echo "Error: ".mysql_errno()."; error description: ".mysql_error()."<br>\n";
}

Then, whenever you want to handle an error, you just call
error_report ();

Queries to Insert Data


Now, look at how you would enter some data into a table. You can use the same mysql_query function you
used previously:


$sql = "INSERT INTO photos VALUES (10000, '99-07-15', 'At the beach, Tobago')";
if ($result = mysql_query ("$sql")) {
echo "Data added\n";
} else error_report ();
With an INSERT, UPDATE, or DELETE query, the integer result of the mysql_query function will be an
integer—positive for success, FALSE for failure.

Running SELECT Queries and Handling the Result


Imagine that you've now added a few more rows of data into your photos table (you can do this yourself
now!). Now comes the time to start interrogating your table to get some data out of it.


Imagine that you want to run a query that searches for all photos for a description that includes the word
"beach". You would write a query like the following:
$sql = "SELECT num, date_taken, description FROM photos WHERE description LIKE
'%beach%'";
if ($result = mysql_query ("$sql")) {
while ($row = mysql_fetch_array ($result)) { echo "Date:
".$row[date_taken]."".$row[description]."<br>\n";
}
} else error_report ();

If all's well, the output should be
Date: 1999-07-15 At the beach, Tobago
Date: 2000-02-01 The beach in Mauritius

Now things are warming up! Examine what happened here.
First, you set the $sql variable to hold your SELECT query. Then you called mysql_query with $sql
as a parameter. This executed the query and captured the result in $result. This result is an integer
that is a pointer to the result set.
If the MySQL server returns an error, $result will be FALSE, so you handle the error. (Note that if the
query returns no rows, this is not an error!)
Provided there's no error, the if statement would resolve to be TRUE, and you would go on to call
mysql_fetch_array.

This introduces you to another function, whose syntax is the following:
mysql_fetch_array (result[, result_type]);
This function returns an array containing the data for the row that was "fetched," or FALSE if no rows
were returned. It stores two lots of the returned data in the array: one lot that can be referenced
numerically ($row[2]), and one lot that can be referenced by using the field names as string keys
($row["description"]).
The optional result_type argument can take the values MYSQL_ASSOC, MYSQL_NUM, or
MYSQL_BOTH. (You probably won't need this in simple scripts like the present example.)
Remember that in PHP every array has an internal pointer? Now it comes in really handy. Each time
you invoke mysql_fetch_array, the pointer helpfully moves itself on by one row, starting the first
time with row zero.
In this way, you can embed the pointer in your while loop without referencing it explicitly. Each time
you invoke mysql_fetch_array and make the $row array equal to its result, the pointer moves
Free download pdf