Menu

Some PHP examples

Developers
2000-03-29
2000-03-29
  • Todd Fennell

    Todd Fennell - 2000-03-29

    Here is an example from the "connect.php" file:

    <?php
        if (!$cn) {
            $dsn = "g27";
            $dbhost = "moby";
            $dbuser = "g27";
            $dbpass = "password";
            $cn = mysql_connect($dbhost, $dbuser, $dbpass);
            mysql_select_db($dsn, $cn);
        }
    ?>

    Variables:

    $cn represents a connection to the database server
    $dsn contains the name of the database (aka data set name)
    $dbhost contains the hostname or IP address of database server
        (in this case 'moby' is the hostname of the sourceforge database server)
    $dbuser is the user to connect as (different access levels can be assigned by user)
    $dbpass is the user's password on the server

    The code:

    What this snippet of code does is set up a connection to the server
    using the mysql_connect() function.  It then specifies the database
    (data set) to use with the mysql_select_db() function.  The part that
    says "if (!$cn)" tells it to do this only if the connection ($cn)
    doesn't already exist.

    ---

    This example is from the "main.php" file:

    <form action='gigpage.php' method='get'>
        <select name=ArtistID id=ArtistID>
        <option selected value=0>-- Select Artist(s) --
        <?php
            $sql = "SELECT ArtistID,ArtistName FROM ArtistTable";
            $sh = mysql_query($sql, $cn);
            while ($artist = mysql_fetch_row($sh)) {
                print "<option value=$artist[0]>$artist[1]\n";
            }
        ?>
        </select>
        <input type=submit value="View Artist(s)">
    </form>

    Variables:

    $sql contains the SQL command to be sent to the database server
    $sh refers to the records returned by the server
        (it's known as a handle but that's not relevant at this point)

    Code:

    SQL is a language of it's own but for the most part it's pretty
    simple to understand (at least it was designed to be.)  The
    SELECT command you see above tells the database server that we want
    it to send us back the ArtistID and ArtistName from each record
    in the ArtistTable.  The mysql_query() function sends the SQL command
    ($sql) to the database server over the connection specified in $cn.
    $cn has already been specified by the time this code snippet is
    executed.

    The line that says "while ($artist = mysql_fetch_row($sh))" does
    a couple different things.  First, it attempts to fetch a record
    from the data returned in $sh and stores it in $artist.  The "while"
    command tells it do this over and over as long as there is new
    data to fetch.  It executes the "print" command each time data is
    successfully 'fetched' into $artist.  So the data in $artist changes
    for each record fetched.

    The two data from each record we fetched into $artist are accessed
    as $artist[0] and $artist[1] (ArtistID and ArtistName.)  If we were
    retrieving more fields (that's what we call elements of a record)
    they would be accessible as $artist[2], $artist[3], and so on.

    The "print" command is that part that actually sends HTML and/or
    text to the web browser (the PHP code is not sent to the web
    browser, only what it prints.)  In this example it's printing
    <option> tags for a <select> element.  The SQL "SELECT" command
    and the HTML <select> element share the same name by coincidence.
    Here's an example of the output:

    <form action='gigs.php' method='get'>
    <select name=ArtistID id=ArtistID>
    <option selected value=0>-- Select Artist(s) --
    <option value=16>Kersah Der Ordin
    <option value=17>Famous Johnson
    <option value=6>KYMYSTRY
    <option value=18>Vicki Fox
    <option value=12>James Solari
    <option value=13>NEFARION
    <option value=14>Jake McCain and Mike Dickey
    <option value=19>Mike
    <option value=21>Andy Grimes
    <option value=22>Like Water
    <option value=23>Wes Cobb
    <option value=24>The Modern Groove Experience
    <option value=25>Jimmy's Hat
    <option value=26>Joshua Leach/Willie Rivers
    </select>
    <input type=submit value="View Artist(s)">
    </form>

    The lines that start with "<option " are the ones that PHP is
    generating from the database.  Well, that's enough for today.
    Any questions so far?  Anyone?  hmmm

     
    • Todd Fennell

      Todd Fennell - 2000-03-29

      I don't know exactly why the variables were cut out in the e-mail message, but they are visible if you read the message on the forum.

       

Log in to post a comment.

MongoDB Logo MongoDB