Frames Modems Help Home Page Chipsets Search No Frames
Diary Entries See also Site Info & Diary.
4 October 2001 Beginning now construction of PHP pages to fill the tables; first, Server-Side Includes and Database connection...
  A great deal of what follows has been inspired or helped by Building a Database-driven Web Site Using PHP on the MySQL website. The Server-Side Includes (SSIs) are at Section 68 & Section 69.

A quick recap on PHP: it is produced by the same people that produce the Apache web-server & is a server-side script interpreter (ASP is a Microsoft equivalent). PHP4 contains support for MySQL within the language. It is possible within a PHP script (PHP page) to do all the things that can be done from the MySQL command line. Thus, SQL commands can be issued to a database & the results can then be parsed (split up into components) & presented on a webpage. Conversely, entries in an HTML form can be sent to a .PHP script and used to fill tables in a database. Sounds easy when you say it like that, doesn't it?

In both the cases above the PHP script needs to connect to the database. This means that the hostname, username, password & database need to be specified - all in plain text. This is fine if everything on the server works OK, as these details never appear on pages sent to the client (usually a browser such as Opera or Microsoft Explorer). If PHP stops working, however, the whole page of script may be delivered up to the browser as-is - not a good idea. The idea of SSIs is to put the confidential info into a separate file & include it into the main script, thus ensuring that the confidential bits are never seen directly.

  In what follows below myHostname etc is replaced in the actual file by the actual name etc (quotes need to be included).
  1. The include file:
      <!-- myIncludeFilename : used to establish connection
         to the MySQL server & database -->
      <?php
        $host="myHostname";
        $user="myUsername";
        $pass="myPassword";
        $data="myDatabase";
      
        if (!$cnx=@mysql_connect($host, $user, $pass)) {
         echo("<P>There is a problem.</P>".
         "<P>Cannot connect to the Database-server at this time.<BR>".
         "This is a Server problem - an email has been sent to the ".
         "Server Support Team.</P>".
         "<P>Try again later.</P>");
         exit();
        }
        if (!@mysql_select_db($data, $cnx)) {
         echo("<P>There is a problem.</P>".
         "<P>Database-server connection has been established but ".
         "connection to the database itself has not.<BR>".
         "An email has been sent to the database administrator.</P>".
         "<P>Try again later.</P>");
         exit();
        }
        $tblList=mysql_list_tables($data);
      ?>
    (The @ symbol in front of mysql_connect & mysql_select_db prevents MySQL error msgs from being displayed & allows me to put in a friendlier error msg of my own. Notice also the dots that act to concatenate (good word, heh?) the various text strings together. Sending the emails will be done later when I've proved that it works.)
  2. A file to test this include file:
      <!-- test.php : used to test myIncludeFilename -->
      <?php
        include("myIncludeFilename");
      
        echo("Yes, we are connected, Jim");
      ?>