Database Reference
In-Depth Information
$results=mysql_query(“query”);
query is a string containing the query we wish to execute.
$results is a variable that stores the results set that the function returns.
So to send a query that selects everything from our log table, we would issue the com-
mand:
$myResults=mysql_query(“SELECT webpageid,browser,ipnumber FROM LOG”);
The above works fine if you are using a static query, such as the one above, because it is
easy to insert the query string into the function. However, if you were executing a dynamic
query, such as searching for a word that was stored in a variable that had been typed in
from a webpage, it is best to store the query in a string, which you can build up as required
first, and then insert this query string into the function as follows:
$searchterm = “Mozilla”;
$query = “SELECT * FROM Log WHERE Browser LIKE '$searchterm%'”;
$myResults=mysql_query($query);
Now the results of the query have successfully been stored in a results set, we can begin
to process these results. If you were running an INSERT query you would send it using
mysql_query and not have to worry about processing the results.
Accessing the Records
We have now stored the records in a record set variable. There are several ways in which you
can obtain the results from this set. We will use mysql_fetch_row as follows:
$variable=mysql_fetch_row($resultsSet)
The variable that this function returns is an indexed array, starting at 0, for every cell in
the current row of the results set. If we were to run the following on our results:
$row=mysql_fetch_row($resultsSet);
The following would output all of the columns in the current row:
print “Row: $row[0], $row[1], $row[3]<BR>”;
The index order in the array corresponds to the order of the columns as specified in the
query, or the creation order of the columns in the CREATE TABLE command if * was used.
If mysql_fetch_row is used a second time on the same results set, it will return the next
row in the set, or FALSE if there are no more rows. Ideally, in use you will want to program
some control structure around the output to handle more than one row. The following will
output all of the results in the record set in a table using a do...while loop:
Search WWH ::




Custom Search