Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Dynamic Web Pages Using PHP and MySQL CS 320.

Similar presentations


Presentation on theme: "Creating Dynamic Web Pages Using PHP and MySQL CS 320."— Presentation transcript:

1 Creating Dynamic Web Pages Using PHP and MySQL CS 320

2 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

3 Review: Web/Database Architecture Network Web browser Web server 2.Request for Web page that requires database data 1.Request for Web page that requires database data 6. Web page HTML file downloaded to client 5. Web page HTML file containing database data downloaded to client Database 3.Runs a program that makes a data request 4. Data response

4 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

5 Why use PHP? PHP uses a similar coding style to other server-side scripting languages (ASP, JSP, Cold Fusion)  If you know how to create a PHP, you won’t find it difficult to learn how to use one of these alternate technologies With a PHP, you can first create a static Web page, then add the commands for dynamic processing  Separates design and program commands (somewhat) Widely used server-side technology  Open source and free  Supports many databases

6 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

7 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

8 CS 320 Test Environment Map a drive to \\leela.cs.uwec.edu\CS320$ \\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

9 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:

10 How are PHP Commands Processed? Commands within the 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

11 General PHP Syntax Notes Every command line must end with ; Comment statements:  // single line  /* block of comments */ echo  Displays a dynamic value on the Web page Supports basic programming constructs:  Variables  If/then statements  Loops  Functions  Assumption: You have learned basic programming concepts in a previous course

12 Example PHP CS 320 - Hello World Hello World! Today's date is:

13 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

14 Using PHP to Retrieve Data From a MySQL Database Steps:  Improve PHP's error messages 1.Connect to the MySQL server 2.Select the MySQL database 3.Send the query to MySQL and store the results 4.Retrieve the number of rows that were returned 5.Close the connection to MySQL 6.Display the results

15 Improving MySQL's Error Messages Add the following as the first line in all of your PHP pages:

16 Connecting to the server and selecting the database In PHPs, you need to use the central DB server  Web server can't access your local database <?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: ". mysql_error()); //Specify the database mysql_select_db("CS320_Student") or die("Could not connect to the database. The reported SQL error is: ". mysql_error()); ?> Database serverUsernamePassword Database within the user schema

17 Specifying the query, storing the result, closing the connection: Notes: Use your local DB to test queries, then copy/paste them into your PHPs Don't use hard returns to format your queries! <?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: ". mysql_error(). " "); //Close the connection mysql_close(); ?>

18 Formatting queries on multiple 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: ". mysql_error()); //Specify the database mysql_select_db("CS320_Student") or die("Could not connect to the database. The reported SQL error is: ". 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: ". mysql_error(). " "); ?> Concatenation operator

19 Displaying the Data Product ID Description Cost Price $ Variable storing a row of retrieved data Database field name


Download ppt "Creating Dynamic Web Pages Using PHP and MySQL CS 320."

Similar presentations


Ads by Google