Accessing Your MySQL Database from the Web with PHP [con't]
|
Choosing a Database to Use
Remember that when you are using MySQL from a command-line interface, you need to tell it which database you plan to use with a command such as
You also need to do this when connecting from the Web. The database to use is specified as a parameter to the mysqli constructor or the mysqliconnect() function. If you want to change the default database, you can do so with the mysqliselect_db() function. It can be accessed as either
or as
Here, you can see the similarity between the functions that we described before: The procedural version begins with mysqli_ and requires the extra database handle parameter.
Querying the Database
To actually perform the query, you can use the mysqli_query() function. Before doing this, however, it's a good idea to set up the query you want to run:
In this case, you search for the user-input value ($searchterm) in the field the user specified ($searchtype). Notice the use of like for matching rather than equal: it's usually a good idea to be more tolerant in a database search.
Tip: Remember that the query you send to MySQL does not need a semicolon at the end of it, unlike a query you type into the MySQL monitor.
You can now run the query:
Or, if you want to use the procedural interface, you use
You pass in the query you want to run and, in the procedural interface, the database link (again, in this case $db).
The object-oriented version returns a result object; the procedural version returns a result resource. (This is similar to the way the connection functions work.) Either way, you store the result in a variable ($result) for later use. This function returns false on failure.
Retrieving the Query Results
A large variety of functions is available to break the results out of the result object or identifier in different ways. The result object or identifier is the key to accessing the rows returned by the query.
In this example, you counted the number of rows returned and also used the
mysqlifetchassoc() function.
When you use the object-oriented approach, the number of rows returned is stored
in the num_rows member of the result object, and you can access it as follows:
When you use a procedural approach, the function mysqlinumrows() gives you the
number of rows returned by the query. You should pass it the result identifier, like this:
It's useful to know this if you plan to process or display the results, because you now know how many there are and can loop through them:
In each iteration of this loop, you call $result->fetchassoc() (or
mysqlifetch_assoc()). The loop does not execute if no rows are returned. This is a
function that takes each row from the resultset and returns the row as an array, with each
key an attribute name and each value the corresponding value in the array:
Given the array $row, you can go through each field and display it appropriately, as
shown in this example:
As previously mentioned, you call stripslashes() to tidy up the value before displaying
it.
Several variations can be used to get results from a result identifier. Instead of an array with named keys, you can retrieve the results in an enumerated array with
or
The attribute values are listed in each of the array values $row[0], $row[1], and so on.
(The mysqlifetcharray() function allows you to fetch a row as either or both kinds
of array. )
You could also fetch a row into an object with the mysqlifetchobject() function:
or
You can then access each of the attributes via $row->title, $row->author, and so on.
Disconnecting from the Database
You can free up your resultset by calling either
or
You can then use
or
to close a database connection. Using this command isn't strictly necessary because the connection will be closed when a script finishes execution anyway.

This chapter is an excerpt from the book, PHP and MySQL Web Development by Luke Welling, Laura Thomson, published by Addison-Wesley Professional, October 2008, ISBN 0672329166, Copyright 2008 Addison-Wesley Professional
Original: November 21, 2008




