1 / 13

PHP

How it Works. Tools. PHP and MySQL

bono
Télécharger la présentation

PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. PHP/MySQL Database-Driven Websites

    2. How it Works The user clicks a link and the request is sent (through routers and switches on the Internet) to the web server which evaluates the request. If the request is for a web page ending in .html, the server just sends that static page back; if the request is for a page ending in .php (or .jsp, .asp, .cfml, or other processing language), it sends the request on to the program (in this case, PHP) to follow the instructions in the request. PHP reads in the instructions sent by the users request, queries the database, retrieves the data and formats it nicely, then sends it to the web server to send on to the user. If the user views the source of the page that is displayed, it will look like regular HTML the user never sees the programming on the .php page.The user clicks a link and the request is sent (through routers and switches on the Internet) to the web server which evaluates the request. If the request is for a web page ending in .html, the server just sends that static page back; if the request is for a page ending in .php (or .jsp, .asp, .cfml, or other processing language), it sends the request on to the program (in this case, PHP) to follow the instructions in the request. PHP reads in the instructions sent by the users request, queries the database, retrieves the data and formats it nicely, then sends it to the web server to send on to the user. If the user views the source of the page that is displayed, it will look like regular HTML the user never sees the programming on the .php page.

    3. Tools PHP and MySQL open source; work on both unix and PC platforms Dreamweaver has built in tools for working with PHP PHP has built in tools for working with MySQL It is easier to do than it first appears Dreamweaver has a few tools for working with PHP, but not many. The important thing is that PHP has the tools for working with the database. But PHP is a powerful programming language as well, and can do more than just read information from a database.Dreamweaver has a few tools for working with PHP, but not many. The important thing is that PHP has the tools for working with the database. But PHP is a powerful programming language as well, and can do more than just read information from a database.

    4. Database Design Make a plan Entity-Relationship Diagrams Use Descriptive Names for Tables Select appropriate data types The linklist example (coming up) uses just one table and is very simple. Even so, I had to decide what information needed to be in the database (such as Title, Description, URL) and how that information should be formatted (as a short bit of text, a long chunk of text, numbers, dates, etc.).The linklist example (coming up) uses just one table and is very simple. Even so, I had to decide what information needed to be in the database (such as Title, Description, URL) and how that information should be formatted (as a short bit of text, a long chunk of text, numbers, dates, etc.).

    5. Build Database (MySQL-Front) Using MySQL-Front, I select New Table, which produces this dialogue box. Here Ive created a Title field that is of type VARCHAR (variable characters, commonly used for short text input) with a length of 120 characters.Using MySQL-Front, I select New Table, which produces this dialogue box. Here Ive created a Title field that is of type VARCHAR (variable characters, commonly used for short text input) with a length of 120 characters.

    6. The Linklist Table Heres a simple table for an annotated link list: It has a unique ID field (the primary key) set to autoincrement whenever a new record is added; CatID is the category; Title, Description, and URL should be self-explanatory; Image is the link to the image on the server, not the image itself. (We dont want the database to get big and slow, so we dont put the images in it, even though it is possible to do so).Heres a simple table for an annotated link list: It has a unique ID field (the primary key) set to autoincrement whenever a new record is added; CatID is the category; Title, Description, and URL should be self-explanatory; Image is the link to the image on the server, not the image itself. (We dont want the database to get big and slow, so we dont put the images in it, even though it is possible to do so).

    7. The Linklist Table: Dataset Heres the table with six entries put in. Note that fields with type of TEXT show (MEMO) rather than the actual content. Heres the table with six entries put in. Note that fields with type of TEXT show (MEMO) rather than the actual content.

    8. Understanding Queries Basic SELECT statement: SELECT * FROM dbname Basic INSERT statement: INSERT INTO dbname (fieldname1, fieldname2) VALUES (value1, value2) Now that we have a database, we need to understand how to ask it for data and how to put data into it. We do this with Structured Query Language (SQL) queries.Now that we have a database, we need to understand how to ask it for data and how to put data into it. We do this with Structured Query Language (SQL) queries.

    9. Writing PHP pages Connect to the Database Perform Queries Get data out from database and turn it into variables Write the variables into formatted output This is the process for getting information out of the database and into a nicely-formatted web page.This is the process for getting information out of the database and into a nicely-formatted web page.

    10. Connecting to the Database <?php # Type="MYSQL" # HTTP="true" $hostname_linklist = "matrix.msu.edu"; $database_linklist = "comp"; $username_linklist = "comp"; $password_linklist = "password"; $ linklist = mysql_pconnect($hostname_ linklist, $username_ linklist, $password_linklist) or die(mysql_error()); $db = mysql_select_db($database_linklist,$ linklist) or die ("Couldn't select database."); ?> This is the code for connecting to the database. Because we have to pass along the password, we keep this in a separate file that we can include in any of the pages that need to connect to the database. This is the code for connecting to the database. Because we have to pass along the password, we keep this in a separate file that we can include in any of the pages that need to connect to the database.

    11. The Link List Example A simple HTML annotated list A database-driven version of the same list Adding records to the database

    12. Link List Example: Code Ive created plain text files of index.php and other files in order to show you the code. index.php input.php processForm.php

    13. Link List Example: Includes Use include files for html that will be used on many pages, and for PHP functions that will be re-used on many pages in the site (such as connecting to the database) htmlheader.inc htmlfooter.inc

    14. Simplify Site Design Use CSS for layouts Use include files for common html elements Use include files for database connection info (security issue)

More Related