* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download PHP
Entity–attribute–value model wikipedia , lookup
Oracle Database wikipedia , lookup
Microsoft Access wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Concurrency control wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Relational model wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Creating Dynamic Web Pages Using PHP and MySQL CS 320 Review: Dynamic vs. Static Web Pages Static Web page Developer specifies contents when he/she creates the Web page Contents do not change based on user inputs Dynamic Web page Content varies based on: User inputs Data retrieved from external sources such as databases Review: Web/Database Architecture 2. Request for Web page that requires database data 1. Request for Web page that requires database data Network 5. Web page HTML file containing database data downloaded to client Web server 3. Runs a program that makes a data request 4. Data response Database 6. Web page HTML file downloaded to client Web browser What is a PHP page? Text file with a .php extension Contains HTML tags and elements Also contains embedded PHP commands that the Web server processes to create dynamic content Why use PHP? PHP uses a similar coding style to other server-side scripting languages (ASP, JSP, Cold Fusion) With a PHP, you can first create a static Web page, then add the commands for dynamic processing If you know how to create a PHP, you won’t find it difficult to learn how to use one of these alternate technologies Separates design and program commands (somewhat) Widely used server-side technology Open source and free Supports many databases Running PHP on a Web Server Web server must support PHP processing You must have privileges to save pages in the Web server folder structure CS 320 Web Development Environment Create PHPs using Dreamweaver Upload to the class Web server for testing Map a drive to a specific folder Save your files in a folder on the server Test your PHPs using a specific URL CS 320 Test Environment Map a drive to \\leela.cs.uwec.edu\CS320$ Save files to your folder in the Students folder URL for testing: http://leela.cs.uwec.edu/CS320/Students/YOU RUSERNAME/filename.php Creating PHPs and PHP Syntax PHP commands are embedded within an HTML document Document must be saved with a .php extension Place PHP script commands within delimiters: <?php PHP commands ?> How are PHP Commands Processed? Commands within the <?php … ?> block are processed on the server before sending the page back to the user's browser The code isn’t sent to the browser – only the results of the code Users can’t View Source to see the code You can’t open a PHP file directly in a browser to run the PHP commands (as you can do with JavaScript) A web server is needed to process PHP commands General PHP Syntax Notes Every command line must end with ; Comment statements: echo Displays a dynamic value on the Web page Supports basic programming constructs: // single line /* block of comments */ Variables If/then statements Loops Functions Assumption: You have learned basic programming concepts in a previous course Example PHP <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>CS 320 - Hello World</title> </head> <body> <p>Hello World! Today's date is: <?php echo date("m/d/y"); ?></p> </body> </html> Using PHP Variables Variable names start with $ Data types include: Booleans (true/false) Integers Floating point numbers Text strings Arrays Objects PHP will make conversions as needed to make data type match the assigned value Using PHP to Retrieve Data From a MySQL Database Steps: Improve PHP's error messages 1. 2. 3. 4. 5. 6. Connect to the MySQL server Select the MySQL database Send the query to MySQL and store the results Retrieve the number of rows that were returned Close the connection to MySQL Display the results Improving MySQL's Error Messages Add the following as the first line in all of your PHP pages: <?php error_reporting(E_ALL); ?> Connecting to the server and selecting the database Database server Username Password <?php //Connect to the MySQL database mysql_connect("dario.cs.uwec.edu","CS320_Student","C4361$") or die("Could not connect to MySQL. The reported SQL error is:<br />" . mysql_error()); //Specify the database mysql_select_db("CS320_Student") or die("Could not connect to the database. The reported SQL error is:<br />" . mysql_error()); ?> Database within the user schema In PHPs, you need to use the central DB server Web server can't access your local database Specifying the query, storing the result, closing the connection: <?php //Steps to connect to the server and select the database //Specify the query $query = "SELECT prod_id, prod_desc, prod_cost, prod_price FROM candy_product"; //Store the result $result = mysql_query($query) or die("SQL Error: <b>" . mysql_error() . "</b><br />"); //Close the connection mysql_close(); ?> Notes: Use your local DB to test queries, then copy/paste them into your PHPs Don't use hard returns to format your queries! Formatting queries on multiple Concatenation operator lines: <?php //Connect to the MySQL database mysql_connect("dario.cs.uwec.edu","CS320_Student","C4361$") or die("Could not connect to MySQL. The reported SQL error is:<br />" . mysql_error()); //Specify the database mysql_select_db("CS320_Student") or die("Could not connect to the database. The reported SQL error is:<br />" . mysql_error()); //Specify the query $query = "SELECT prod_id, prod_desc, prod_cost, prod_price " . "FROM candy_product"; //Store the result $result = mysql_query($query) or die("SQL Error: <b>" . mysql_error() . "</b><br />"); ?> Displaying the Data Variable storing a row of retrieved data <table border="4" cellpadding="3" cellspacing="1"> <tr><th>Product ID</th> <th>Description</th><th>Cost</th><th>Price</th></tr> <?php while ($row = mysql_fetch_array($result)) { ?> <tr> <td><?php echo $row["prod_id"] ?></td> <td><?php echo $row["prod_desc"] ?></td> <td>$<?php echo $row["prod_cost"] ?></td> <td>$<?php echo $row["prod_price"] ?></td> </tr> <?php } ?> </table> Database field name