|
General Clean Up resets the |
Person Table |
Book Table |
Documentation |
||
|
Note - Create, Drop and Clean Up would be
hidden on a real life system. |
|||||
| PHP TAGS |
|
| <?php |
PHP scripts begin and end with these tags. The PHP preprocessor executes all the statements enclosed between these tags. Anything not enclosed is treated as normal HTML or text. |
INCLUDING FILES |
|
require "filename"; |
Include the named file as if it were part of the file it is being included into. The script fails if the file can not be found. |
include "filename"; |
Include the named file as if it were part of the file it is being included into. Gives a warning message if the file can not be found but the script continues. |
COMMENTS |
|
<!-- html comment --> |
This is an HTML comment. It will be ignored by the web browser. |
// PHP Comment |
This is a PHP comment. It will be ignored by the PHP preprocessor. The comment ends at the end of the line. |
/* PHP Comment */ |
This is a PHP comment. It will be ignored by the PHP preprocessor. The comment ends at the closing */ marker. This style of comment can span several lines. |
FILE HANDLING |
|
$fp = fopen($fileName, "r"); |
Open the file named by $fileName for reading ("r"). $fp is a file pointer that is used to refer to the file until it is closed later. |
fclose($fp); |
Close the file referred to by $fp. |
while (!feof($fp)) } |
! means NOT. feof tests for the end of file. $fp is a pointer to the file being tested. The entire statement repeats until the end of file is encountered. |
$in = fgets($fp,
4094); |
File Get String. Read a line of text from the file that
$fp refers to. Store the line of text into the $in variable. |
TEXT OUTPUT |
|
echo "<table cellpadding=\"10\">\n"; |
echo is used to send text to the standard output. For web based scripts, this is the html sent to the web browser. The text being sent is enclosed in "double" or 'single' quotes. If 'single' quotes are used, the enclosed text is simply sent to the browser. If "double" quotes are used, any embedded variables are processed and their values returned. This carries a processing overhead and should be avoided unless there are embedded variables. Ignoring this rule is common. \n sends a new line to the output stream. \" sends a quote to the output stream. Without the \ the quote would close the text string. |
< |
< begins an html tag. To display a < use < |
MySQL STATEMENTS - Connecting, selcting a database and closing. |
|
|
$link = mysql_connect($host, $user, $password) or die(mysql_error()); mysql_select_db ($database); mysql_close($link); |
|
Connect to a MySQL database. Store a reference to the connection in $link. The $host variable usually contains the value "localhost". The $user variable might be "root" or another user name assigned to you or by you. In general only major administration should be carried out by the root user. The $password variable contains the password known only to you and possibly the MySQL administrator. or die(mysql_error()) echoes an error message and kills the script if something goes wrong. mysql_select_db() is used to specify which database to use. MySQL can manage many different databases. $database is a variable which contains the name of the database. In this example the database is named "aardvark". mysql_close($link) is used when you have finished with the connection. If you forget to do this, PHP will eventually clean up after you. |
|
MySQL STATEMENTS - EXECUTING SQL Queries |
|
| $sql = "DROP
DATABASE aardvark"; $result = mysql_query($sql, $link) or die(mysql_error()); |
|
DROP DATABASE is used to delete an entire database. aardvark is the name of the database. mysql_query is used to run the SQL. $sql contains the SQL statement. $link contains the reference to the database connection. or die(mysql_error()) echoes an error message and kills the script if something goes wrong. |
|
MySQL STATEMENTS - SQL Examples |
|
$sql = "DROP DATABASE aardvark"; |
Delete the entire databse. |
$sql = "CREATE DATABASE aardvark" |
Create the database (there will be no tables at this stage). |
$sql
= "CREATE TABLE person CREATE TABLE person
- creates a table named "person". The table has five columns. |
|
$sql
= "INSERT INTO person INSERT INTO person
- this SQL code inserts a record into the person table. |
|
$DELETE
FROM DELETE FROM person - This deletes from the person table. |
|
HTML Forms and $_POST |
|
<form
name="form1" method="post" action=""> |
|
<?php echo
$_POST['title']; ?>
|
When a page is loaded, if any data was posted from a form,
it is available to PHP. |
foreach
($_POST as $key => $value) foreach - is used to repeat and visit each location
in an array. |
|
$sql
= "SELECT * FROM person"; Here an SQL query is selecting all the columns (*) from the person table. |
|