johndoe
with a password of ThePassword
. The database is named
Acme
and contains a table named Accounts
.<?php $con = mysql_connect("localhost","johndoe","ThePassword"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("Acme", $con); $accounts = mysql_query("SELECT * FROM Accounts"); ?>
The above code, which is included on a webpage, selects all of the records in the table. The code below calls a function that displays each selected record, i.e., all records, since all records were selected above.
<?php // Display accounts while($account = mysql_fetch_array($accounts)) { displayAccount($account); } ?>
The code below, which is included near the top of the web page, provides a function that displays individual records from the table.
<?php function displayAccount($account) { echo "<tr>\n"; echo "<td><a href=\"" . $account['URL'] . "\">" . $account['ID'] . "</a></td>"; echo "<td>" . $account['Description'] . "</td>"; echo "<td>" . $account['Category'] . "</td>"; echo "<td>" . $account['Type'] . "</td>"; echo "<td>" . $account['Note'] . "</td>\n"; echo "</tr>\n\n"; } ?>
The following fields are displayed for each record: ID, Description,
Category, Type, and Note. The ID is displayed as a clickable link that
leads to a webpage stored as a
URL in the table column named URL. Where a double quote shuold appear in
the output HTML, e.g., <a href="someurl"
, the double quote is
preceded by a
backslash, which serves as an
escape character,
i.e., it prevents PHP from assuming the double quote is part of the PHP
code, but, instead, instructs it to treat it as just any other character
without any special meaning.
Supposing that there are times when I don't want to see every entry in
the table, but only the record corresponding to a specific ID, I could use
http://example.com/acme/accounts.php
to have the code above
executed to display all records in the Accounts table in the Acme database,
but specify a parameter with the URL when I only want to display an entry
corresponding to a specific ID with
http://example.com/acme/accounts.php?id=acctid
where I
would substitute a specific account's id in place of acctid, e.g.,
http://example.com/acme/accounts.php?id=A014VEONP7
. Above I have
the line:
$accounts = mysql_query("SELECT * FROM Accounts");
Instead of just that line, I could have the following code on the page:
if (isset($_GET["account"])) { $chosen = $_GET["id"]; $accounts = mysql_query("SELECT * FROM Accounts WHERE ID = '{$chosen}'"); } else { $accounts = mysql_query("SELECT * FROM Accounts"); }
The PHP code checks to see if the parameter account
has been
passed with if (isset($_GET["account"]))
. If it has been passed
in the URL and is thus set, then only records where the account ID matches
the value passed in the URL with ?id=
will be displayed, since
the list of selected records passed to the displayAccts
function
will contain only the record, or records, with the specified ID. This is done
by setting a variable named chosen
to the value of the
id
parameter and then using WHERE ID =
followed
by that variable name enclosed withing single quotes and
braces.
If I wanted to display that record, or records, differently than when
I display all records on the webpage, I could create a separate function
to handle the display and call it, instead of the displayAccts
function.