Php/Mysql Cheat Sheet
Php/Mysql Cheat Sheet
There is a lot to learn about the interaction between PHP and MySQL.
But the basic tools can be covered relatively quickly. This handout is
designed to be a quick-reference guide for the most important features of
PHP and MySQL.
Identifying PHP code
PHP code intermixed with HTML is delimited by the tags <?php ... ?>. PHP
statements end with a semi-colon.
Variables
Variables are simply names used in PHP to store values. They are indicated by a string
preceded by a dollar sign. For instance, the following statement assigns the value
“CSC111” to the variable $course_number:
$course_number = “CSC111”;
Variables in PHP do not have to be declared before they can be used.
Output
Information is written to the browser window in PHP using the echo or print
command. The two commands are interchangeable. HTML tags mixed in with the
output to be printed are interpreted appropriately. The period character (“.”) is used to
concatenate parts of output strings together. For example:
echo “The sum of ” . $num1 . “ and ” . $num2 . “ is ” . $sum . “.”;
As long as you use double-quotes, you can simplify statements like this by including the
variables within the quotes. Then the values of the variable will replace the variables
themselves. For instance, the statement:
echo “The sum of $num1 and $num2 is $sum”;
will print
The sum of 3 and 5 is 8
assuming $num1=3, $num2=5 and $sum=8.
It is good PHP programming practice to check to make sure a query was successful. This
can be done with the following code:
if (!$allValues) {
echo “Could not execute query ($SQL): “ . mysql_error();
exit;
}
The die() function could also be used here, as in the previous example.
If the query was unsuccessful, the variable $allValues will contain the value FALSE.
So this will print an error message, the original query, and an explanation of the error in
the event of a failed query. It will then terminate the application.
Keene/Treu, 2001
CSC-111: Introduction to Information Technology page 3
If the query is successful, there is a default pointer that at any given moment is pointing
to one of the retrieved records in the resource (initially the first one). Consider the
following line of code:
$thisValue = mysql_fetch_array($allValues)
The function mysql_fetch_array grabs a single row of data from the
$allValues variable and stores it in $thisValue. Now you can access the data
from the record just by using the original field names. For instance:
echo $thisValue[“field1”];
echo $thisValue[“field2”];
The nice thing about the function mysql_fetch_array is that it automatically moves
the pointer to the next record. It also returns a TRUE/FALSE value that lets you know
when it has reached the end of the data. This is useful for looping through data. For
instance:
$allValues = mysql_query(“SELECT * FROM Grades”, $linkID);
while ($thisValue = mysql_fetch_array($allValues))
{
echo $thisValue[“field1”] . “ ” . $thisValue[“field2”];
echo “<BR>”;
}
The extract function pulls the values of each row out into variables named after the
actual field names. Try this alternate version if you like.
Keene/Treu, 2001