Download presentation
Presentation is loading. Please wait.
Published byPetri Heikkinen Modified over 6 years ago
1
PHP Overview PHP: Hypertext Preprocessor Server-Side Scripting
Code also included with HTML Pre-processed on server Browser gets only HTML and client-side code More secure than JavaScript More powerful than JavaScript File access, database access, etc. Less efficient than JavaScript for interactive features Must “go back” to the server When not doing JavaScript, just refer to the behaviors we’ve seen in lab.
2
PHP Overview Open source software Free, users can add features
E.g., phpMyAdmin, DBMS connectivity Evolving documentation ( Competitors Active Server Pages (Microsoft) Java servlets and JSP (Sun) Cold Fusion (Macromedia)
3
PHP Overview Summary Secure method for Processing form data
More extensive calculations, manipulations possible Selective display of database data Updates, deletions from databases Separating content from design Keeping track of information from one web page to another ing information Validating users (logins, privileges, etc.) Etc.
4
PHP Overview Basic examples Output Variables and assignment
Use of built-in functions (and php.net) User-written functions Control structures Processing form input Processing querystring input POST and GET methods Session variables
5
PHP Overview Two DB examples (quick look first)
Try it Two DB examples (quick look first) List all customer information from database Design: Connect to the DB server Select the DB we want to use (“CS19”) Get the Customer information from the DB Display it using HTML Disconnect from the DB Note: Compare to the steps we use to directly connect to the DB using phpMyAdmin
6
PHP Overview Connect to the DB server and select the DB “CS19”
include ("/home/ktreu/aux/common_db.inc"); $linkID = mysql_connect("localhost",$dbusername,$dbuserpassword) or die ("Could not connect: " . mysql_error()); mysql_select_db("CS19", $linkID) or die ("Unable to select db: " . mysql_error()); Notes: php delimiter, variables, including external files, error checking
7
PHP Overview Get the list of Customers from the DB $SQL = "SELECT * FROM Customers ORDER BY name"; $allValues = mysql_query($SQL, $linkID); if (!$allValues) { echo "Could not successfully run query ($SQL) from DB: " . mysql_error(); exit; } Notes: use of SQL, possibility of multiple links to DB server, built-in MySQL functions, error checking, contents of $allValues
8
PHP Overview echo "<h4>All Customers Ordered by Last Name</h4>"; echo "<TABLE BORDER=1 CELLPADDING=8>"; echo "<TR><TD><B>Customer Number</B></TD><TD><B>Name</B></TD><TD><B>Phone</B></TD>"; while ($thisValue = mysql_fetch_array($allValues)) { echo "<TR>"; echo "<TD>" . $thisValue["custnum"] . "</TD>"; echo "<TD>" . $thisValue["name"] . "</TD>"; echo "<TD>" . $thisValue["phone"] . "</TD>"; echo "</TR>"; } echo "</TABLE>"; mysql_close($linkID); ?> Notes: printing HTML, looping, accessing each record from the query one by one, closing the connection
9
PHP Overview What changes would be needed to get different data?
List all Product information? List all products sold in March with the name of the customer who purchased them? Etc.
10
PHP Overview Another example (quick look)
Try it Another example (quick look) List all customer information from database for a specific customer Design: Get the desired customer number from the user Connect to the DB server Select the DB we want to use (“CS19”) Get the information for the customer with the desired customer number from the DB Display it using HTML Disconnect from the DB
11
PHP Overview Notes: method and action attributes of the form
Write a form to get the customer number <form method="post" action="listCustomersByCustnum.php"> Please enter a Customer Number: <input type="text" name="custnum" length="4" maxlength="4"> <br> <input type="submit" value="Click for Customer Info"> </form> Notes: method and action attributes of the form
12
PHP Overview Only two changes necessary in the PHP code $custno = $_POST['custnum']; … $SQL = "SELECT * FROM Customers WHERE custnum=$custno"; $allValues = mysql_query($SQL, $linkID); if (!$allValues) { echo "Could not successfully run query ($SQL) from DB: " . mysql_error(); exit; } Notes: Accessing the form data, using it in a query, significance of double quotes
13
Additions Edit Delete Insert Admin functions Other tips and tricks
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.