Specifications

In this case, we are searching for the user-input value ($searchterm) in the field the user speci-
fied ($searchtype). You will notice that we have used like for matching rather than equalits
usually a good idea to be more tolerant in a database search.
Using MySQL
P
ART II
236
Its important to realize that the query you send to MySQL does not need a semicolon
on the end of it, unlike a query you type into the MySQL monitor.
TIP
We can now run the query:
$result = mysql_query($query);
The mysql_query() function has the following prototype:
int mysql_query(string query, [int database_connection] );
You pass it the query you want to run, and optionally, the database link (again, in this case
$db). If not specified, the function will use the last opened link. If there isnt one, the function
will open the default one as if you had called mysql_connect().
You might want to use the mysql_db_query() function instead. It has the following prototype:
int mysql_db_query(string database, string query, [int database_connection] );
Its very similar but allows you to specify which database you would like to run the query on.
It is like a combination of the mysql_select_db() and mysql_query() functions.
Both of these functions return a result identifier (that allows you to retrieve the query results)
on success and false on failure. You should store this (as we have in this case in $result) so
that you can do something useful with it.
Retrieving the Query Results
A variety of functions are available to break the results out of the result identifier in different
ways. The result identifier is the key to accessing the zero, one, or more rows returned by the
query.
In our example, we have used two of these: mysql_numrows() and mysql_fetch_array().
The function mysql_numrows() gives you the number of rows returned by the query. You
should pass it the result identifier, like this:
$num_results = mysql_num_rows($result);
13 7842 CH10 3/6/01 3:36 PM Page 236