Programming with php By: Seth Larson A little bit about PHP PHP stands for PHP: Hypertext Preprocessor PHP is a widely-used general-purpose server-side scripting language that is suited for Web development and can be embedded into HTML. PHP scripts are executed on the server PHP supports many different databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software (OSS), so its free PHP files have a file extension of ".php", ".php3", or ".phtml" PHP files are returned to the browser as plain HTML Server Side o o o o o <html> <body> <?php echo '<p>PHP rules!</p>';?> </body> </html> Client Side <html> <body> <p>PHP rules!</p> </body> </html> What do you need installed on your computer to use php? A server that supports php. Some servers that support php are Apache(open source), IIS (Microsoft), and Oracle. The latest version of PHP found at http://www.php.net You can test if php is installed and working with the following code. Show php information <html> <body> <?php phpinfo(); ?> </body> </html> Some PHP syntax A php statement always begins with <?php and ends with ?>. php statements can be placed anywhere in the document. Statements end with semicolon. PHP variables Variables start with $, example $var Variables have the same syntax rules as other high level languages. To concatenate two or more variables together, use the dot (.) operator: Using the “\” right next to a variable name tells the server to print what directly follows and not to treat it as a label. Sample Variables <html> <body> <?php $var_A = " Print \$var_A "; $var_B = "To print a variable name instead of what the variable is holding use the \ character."; echo $var_B; echo"<P>"; echo $var_A; ?> </body> </html> PHP operators Arithmetic Operators +, -, /, * Assignment Operators =, +=, -=, *=, /=, %= Comparison Operators ==, !=, >, <, >=, <= Logical Operators &&, ||, ! If – else statement if (condition) code to be executed else code to be executed While while { Do-while do { For Foreach code to be executed; code to be executed; } } while (condition); PHP forms Form is created with html. Any html element you put in a form is available after a submit because php automatically makes element variables. Examples: $_POST["first"],$_POST["last" Notice that if you use post in the form header the variables are accessible with $_POST, if you use get the variables are accessible with $_GET. Form <html> <body> <form action="php_forms_next.php" method="post"> <pre>Your Id: <input type = "text" name = "id"/> </ br> Your First name: <input type="text" name="first" /> </ br> Your Last name: <input type="text" name="last" /> <?php echo "male: <input type = checkbox name = sex value male> Female: <input type = checkbox name= sex value=female> ?> <input type="submit" /></pre> </form> </body> </html> Print form variables <html> <body><pre>You said your Id number was: <?php echo $_POST["id"]; ?></pre> <pre>You said your First name was <?php echo $_POST["first"]; ?></pre> <pre>You said you Last name was <?php echo $_POST["last"]; ?></ pre> <pre>You also said you are <?php echo $_POST["sex"];?></pre> <pre>Your favorite color is <?php echo $_POST["color"];?></pre> </body> </html> Text File Read/Write <html> <body> <?php if (!($file=fopen("php_file.txt","r"))) exit("Can’t open file."); while (!feof($file)) { $char=fgetc($file); echo $char; } //$write = fputs($fp, “write hi”); fclose($file); ?> </body> </html> Using a database The mysql_connect() function opens a link to a MySQL server on the specified host along with a username (root) and a password. mysql_select_db() is used to tell my MySQL to use the specified database. mysql_query()sends a line of SQL to the MySQL database identified in the connection for processing. The returned results are stored in $result. mysql_result() is used to display the values of fields from our query. %s indicates that the variable in the second half of the expression (e.g., mysql_result($result,0,"position")) should be treated as a string and printed. Reading one field from each record in the database <html> <body> <?php $db = mysql_connect("localhost", "root","seth"); mysql_select_db("mydb",$db); $result = mysql_query("SELECT * FROM employee",$db); $count=mysql_numrows($result); $i=0; while ($i < $count) { printf("First Name: %s", mysql_result($result,$i,"first")); echo "<p>"; $i++; } mysql_close(); ?> </body> </html> Reading records one at a time <html> <body> <?php $db = mysql_connect("localhost", "root", "seth"); mysql_select_db("mydb",$db); $result = mysql_query("SELECT * FROM employee",$db); echo "<table border=1>\n"; echo "<tr><td>id</td><td>First Name and Last Name</td></tr>\n"; while ($myrow = mysql_fetch_row($result)) { printf("<tr><td>%s </td><td>%s %s</td></tr>\n", $myrow[0], $myrow[1], $myrow[2]); } echo "</table>\n"; ?> </body> </html> Inserting into a Database: form part <html> <body> <form action="php_insert_form_insert.php" method="post"> <pre> Your Id: <input type = "text" name = "id"/> Your First name: <input type="text" name="first" /> Your Last name: <input type="text" name="last" /> </br> <input type="submit" /></pre> </form> </body> </html> Insert into a MySQL database <html><body> <?php $id = $_POST["id"]; $first = $_POST["first"]; $last = $_POST["last"]; $db = mysql_connect("localhost", "root","seth"); mysql_select_db("mydb",$db); $result = mysql_query("SELECT * FROM employee",$db); echo "<table border=1>\n"; echo "<tr><td>id</td><td>First Name and Last Name</td></tr>\n"; while ($myrow = mysql_fetch_row($result)) { printf("<tr><td>%s </td><td>%s %s</td></tr>\n", $myrow[0], $myrow[1], $myrow[2]); } echo "</table>\n"; $query = "INSERT INTO employee VALUES ('$id','$first','$last')"; mysql_query($query); ?> </body> </html>