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

(singke) #1

$name=~ s/%(..)/pack("c",hex($1))/ge;


if (!defined($field{$name})) {


$field{$name}=$val;


}


else {


$field{$name} .= ",$val";


}

}

}

return 1;


}


There is a lot of code in this listing. The interesting part is the building of the SQL statement. This
statement is generated based on the values the user chose. Simple if statements control what goes
into the SQL statement. The SQL statement is a simple SELECT statement that joins on the Ads table.
You do this because you only want customers who have posted ads. This guarantees it. The result set
is stored in the $sth variable. If there were no errors, the code continues by building the result page.
This page will contain a grid of data with alternating row colors. This is made possible by checking to
see if a row is odd or even. If the row is odd, the background color is turned to a dark gray and the font
color is turned to white. If the row is even, the background color is white and the font color is black. This
neat little trick is simple to do, and it greatly enhances the page that is returned to the user.
The cells in the table are filled with the values contained in the record set. The while loop allows you to
walk through the result set and post the values that were in the SELECT query.

Figure 21.7 Search ad results.

Free download pdf