Book HomeMySQL and mSQLSearch this book

12.3. PHP

By its very nature, W3-mSQL is highly specialized for use with the mSQL database server. If you are using MySQL, or if your needs are not covered by W3-mSQL, there are other HTML preprocessors available that offer database support.

PHP, which stands for "PHP: Hypertext Preprocessor," is an application very similar to W3-mSQL in spirit. They are both CGI programs that interpret HTML before sending a final page to the browser. They both have their own built-in scripting language. Moreover, they both have tightly integrated database capabilities. However, PHP extends beyond the range of W3-mSQL by offering compatibility with several database servers, including both MySQL and mSQL.

PHP's scripting language is also more extensive, covering more possible applications than W3-mSQL. In short, you should use PHP unless you are definitely wedded to mSQL as a database server, in which case some of W3-mSQL's optimizations may suit you.

If you use PHP, the HTML example shown earlier which retrieve information from a shark database would now look as follows:

<HTML>
<HEAD><TITLE>Shark Search Result</title></head>
<BODY>
<H1>Here are the sharks that match your search...</h1>
<p>
<?
/* We now start to build the query. When finished, a typical query
* will look something like this:
* SELECT * FROM SHARK WHERE SPECIES='Isurus Paucus' AND AGE=2
*/

   $query = "select * from sharks where ";

   if ($species || $age || $location) {
      $query += " where ";
   }

   if ($species) { $query += "species = '$species'"; }
   if ($age) {
      if ($species) { $query += " and "; }
      $query += "age = $age";
   }
   if ($location) {
      if ($species || $age) { $query += " and "; }
      $query += "location = '$location'";
   }
   $result = msql("sharks",$query);
   if (result == -1) {
      echo("Error : $phperrmsg\n");
      exit(1);
   }
   $numresults = msql_numrows($result);
>

<UL>

<!
   if (! $numresults );
>
 <H2>No results matched</h2>
<!
   else {
      while ($i < $numresults) {
         $id[$i] = msql_result($result,$i,"id");
         $species[$i] = msql_result($result,$i,"species");
         $age[$i] = msql_result($result,$i,"age");
         $loc[$i] = msql_result($result,$i,"location");
         echo("<LI>");
         printf("<IMG SRC=\"graphics/shark%s.gif\" ALIGN=LEFT>", $id[$i]);
         echo("<B>Species:</b> $species[$i]<br>");
         if ($age[$i] == 1) { $age = "Young"; }
         else if ($age[$i] == 2) { $age = "Adult"; }
         else if {$age[$i] == 3) { $age = "Old"; }
         echo("<B>Age:</b> $age<br>");
         echo("<B>Location</b> $location[$i]<br>");
      }
   }
>
</ul>
<A HREF="search.html">Search again</a>
</body></html>


Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.