The php scripting language is a method of accessing your
MySQL database via the Web. Below is an Example program using
PHP to interact with a MySQL database, which has been commented to explain each function:
// Connect To Database
// * mysql_connect takes the servername, user, and password
// * as arguments. mysql_selectdb takes the database name.
// * Together, they open a connection to your database.
mysql_connect($SERVER,$USER,$PASSWORD);
mysql_selectdb($DATABASE);
// Execute Query
// * mysql_query takes as its argument the query you are
// * executing on the database. It should be assigned to
// * a variable -- the variable is used by other functions
// * to retrieve the results.
$QUERY = mysql_query("SELECT * from test");
// How man rows in results?
// * mysql_num_rows takes the variable the query was
// * assigned to (referred to hereafter as the query
// * identifier) and returns the number of rows the query
// * resulted in.
$NUMROWS = mysql_num_rows($QUERY);
// Display Results
if ($NUMROWS) {
$I = 0;
while ($I < $NUMROWS) {
// Get Results
// * mysql_result returns the value of a specific field
// * in a specific row. It takes three arguments: the
// * first is the query identifier, the second is the row
// * number, and the third is the field name. In this
// * example, a while loop is used to process all
// * rows.
$FIELD1 = mysql_result($QUERY,$I,"field1");
$FIELD2 = mysql_result($QUERY,$I,"field2");
$FIELD3 = mysql_result($QUERY,$I,"field3");
echo "field1 = $FIELD1, field2 = $FIELD2, field3 = $FIELD3 \n";
$I++;
}
}
?>
PHP provides many other MySQL-related functions.