+ All Categories
Home > Documents > Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some...

Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some...

Date post: 21-Dec-2015
Category:
View: 223 times
Download: 1 times
Share this document with a friend
73
Intermediate PHP & MySQL
Transcript
Page 1: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

Intermediate PHP & MySQL

Page 2: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

Welcome

This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.It is the second of two PHP workshops available at www.tinyurl.com/rpi123. In addition to the two PHP workshops, there are also workshops on HTML and CSS.

These slides are based on source material found at the w3schools.com website.You are encouraged to visit the site – it is a great resource.

Page 3: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

Let's dive right in, shall we?

Page 4: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP Date() Function

The PHP date() function formats a timestamp to a more readable date and time.

Page 5: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP Date() Function

The required format parameter in the date() function specifies how to format the date/time.

d - Represents the day of the month (01 to 31) m - Represents a month (01 to 12) Y - Represents a year (in four digits)

Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting.

Page 6: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP Date() Function

Page 7: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP Date() Function

The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used.

The mktime() function returns the Unix timestamp for a date.

The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Page 8: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP Date() Function

Page 9: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP Server Side Includes (SSI)

You can insert the content of one PHP file into another PHP file before the server executes it, with the include() or require() function.

> include() generates a warning, but the script will continue execution> require() generates a fatal error, and the script will stop

Page 10: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP Server Side Includes (SSI)

These two functions are used to create functions, headers, footers, or elements that will be reused on multiple pages.

SSI saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. When the header needs to be updated, you update the include file, or when you add a new page to your site, you can simply change the menu file (instead of updating the links on all your web pages).

Page 11: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP include() Function

The include() function takes all the content in a specified file and includes it in the current file.

If an error occurs, the include() function generates a warning, but the script will continue execution.

Here's what the error output might look like...

Page 12: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP include() Function error

Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/user/public_html/page.php on line xx

Warning: include(http://www.somedomain.com/file.php) [function.include]: failed to open stream: no suitable wrapper could be found in /home/user/public_html/page.php on line xx

Warning: include() [function.include]: Failed opening 'http://www.somedomain.com/file.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/user/public_html/page.php on line xx

Page 13: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP include() Function

Assume that you have a standard header file, called "header.php". To include the header file in a page, use the include() function:

Page 14: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP include() Function

Assume we have a standard menu file, called "menu.php", that should be used on all pages:

And this is how you'd include it...

Page 15: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP: include() Function

Here's what the output HTML would look like...

Page 16: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP: include() Function

Page 17: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP require() Function

The require() function is identical to include(), except that it handles errors differently.

If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

Page 18: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP require() Function

So, if we use include()...

Page 19: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP require() Function

The errors show, but the code still gets executed. See how it echoed “Hello World”.

Page 20: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP require() Function

But, if we use require() instead...

Page 21: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP require() Function

We get only the error message – and the code doesn't get executed.

Page 22: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP Cookies

A cookie is often used to identify a user.A cookie is a small file that the server embeds on the user's computer.Each time the same computerrequests a page with a browser,it will send the cookie, too. WithPHP, you can both createand retrieve cookie values.

Page 23: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP setcookie() Function

The setcookie() function is used to set a cookie.

Note: The setcookie() function must appear BEFORE the <html> tag.

Page 24: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP setcookie() Function

In the example below, we will create a cookie named "user" and assign the value "Alex Porter" to it. We also specify that the cookie should expire after one hour:

Page 25: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP setcookie() Function

You can also set the expiration time of the cookie in another way. It may be easier than using seconds.

Page 26: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP $_COOKIE

The PHP $_COOKIE variable is used to retrieve a cookie value.

In the example below, we retrieve the value of the cookie named "user" and display it on a page:

Page 27: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

PHP $_COOKIE

If the cookie is set, receive a personal welcome – if not, it echoes a generic greeting.

Page 28: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

And now for something

Completely different...

Page 29: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

(Instant “A” for those who got the Monty Python reference)

A brief but fascinating introduction to MySQL

Page 30: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

FYI The Logo

The new MySQL logo - a jumping dolphin - symbolizes the speed, power, precision and good nature of the MySQL database and community.

Michael "Monty" Widenius (creator of MySQL):"I am personally concerned about the survival of endangered species, and I liked the idea of the dolphin as soon as it came up. It combines great symbol value with a powerful, modern design."

Page 31: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

FYI The Name

The My in MySQL is not like the i in iPad.

It is actually the name of Monty's daughter.Yes, her name is My.

The SQL part stands for Structured Query Language.

Page 32: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Introduction

MySQL is a database.

The data in MySQL is stored in database objects called tables.

A table is a collection of related data entries and it consists of columns and rows.

Page 33: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Tables

A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data.

Below is an example of a table called "Persons":

Page 34: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Queries

A query is a question or a request.

With MySQL, we can query a database for specific information and have a recordset returned.

Look at the following query:

Page 35: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Queries

The query above selects all the data in the "LastName" column from the "Persons" table, and will return a recordset like this:

Page 36: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL mysql_connect()

Before you can access data in a database, you must create a connection to the database.

In PHP, this is done with the mysql_connect() function.

Page 37: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL mysql_connect()

In the following example we store the connection in a variable ($con) for later use in the script. The "die" part will be executed if the connection fails:

Page 38: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL mysql_close()

The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function:

Page 39: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Create a Database

The CREATE DATABASE statement is used to create a database in MySQL.

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Page 40: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Create a Database

Page 41: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Create a Table

The CREATE TABLE statement is used to create a table in MySQL.

We must add the CREATE TABLE statement to the mysql_query() function to execute the command.

Page 42: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Create a Table

This is the top part of the file.The CREATE TABLE code is on the next slide...

Page 43: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Create a Table

Page 44: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Create a Table

Important: A database must be selected before a table can be created. The database is selected with the mysql_select_db() function.

Note: When you create a database field of type varchar, you must specify the maximum length of the field, e.g. varchar(15).

Page 45: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Create a Table

About Primary Keys and Auto Increment Fields:

Each table should have a primary key field.

A primary key is used to uniquely identify the rows in a table. Each primary key value must be unique within the table. Furthermore, the primary key field cannot be null because the database engine requires a value to locate the record.

Page 46: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Create a Table

The following example sets the personID field as the primary key field. The primary key field is often an ID number, and is often used with the AUTO_INCREMENT setting. AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added. To ensure that the primary key field cannot be null, we must add the NOT NULL setting to the field.

Page 47: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Create a Table

Page 48: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Inserting Data

The INSERT INTO statement is used to add new records to a database table.Syntax

It is possible to write the INSERT INTO statement in two forms.

Page 49: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Inserting Data

The first form doesn't specify the column names where the data will be inserted, only their values:

The second form specifies both the column names and the values to be inserted:

Page 50: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Inserting Data

To get PHP to execute the statements above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Previously we created a table named "Persons", with three columns; "Firstname", "Lastname" and "Age". We will use the same table in this example. The following example adds two new records to the "Persons" table:

Page 51: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Inserting Data

Page 52: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Inserting Data - forms

Now we will create an HTML form that can be used to add new records to the "Persons" table.

Page 53: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Inserting Data - forms

When a user clicks the submit button in the HTML form in the example above, the form data is sent to "insert.php".

The "insert.php" file connects to a database, and retrieves the values from the form with the PHP $_POST variables.

Then, the mysql_query() function executes the INSERT INTO statement, and a new record will be added to the "Persons" table.

Page 54: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Inserting Data - forms

Page 55: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Selecting Data

The SELECT statement is used to select data from a database.

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Page 56: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Selecting Data

Page 57: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Selecting Data

The example above stores the data returned by the mysql_query() function in the $result variable.

Next, we use the mysql_fetch_array() function to return the first row from the recordset as an array. Each call to mysql_fetch_array() returns the next row in the recordset. The while loop loops through all the records in the recordset. To print the value of each row, we use the PHP $row variable ($row['FirstName'] and $row['LastName']).

Page 58: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Selecting Data

The output of the code from two slides back is:

Here's how to return the data in an HTML table:

Page 59: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Selecting Data

Page 60: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Selecting Data

Page 61: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

Here's another cool table...

Page 62: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Selecting Data

The WHERE clause is used to extract only those records that fulfill a specified criterion.

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Page 63: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Selecting Data

Page 64: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Ordering

The ORDER BY keyword is used to sort the data in a recordset.

The ORDER BY keyword sorts the records in ascending order by default.

If you want to sort the records in a descending order, you can use the DESC keyword.

Page 65: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Ordering

Page 66: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Ordering

It is also possible to order by more than one column. When ordering by more than one column, the second column is only used if the values in the first column are equal:

Page 67: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Updating

The UPDATE statement is used to update existing records in a table.

Page 68: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Updating

Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Page 69: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Updating

Page 70: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Deleting

The DELETE FROM statement is used to delete records from a database table.

Page 71: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Deleting

Note: Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Page 72: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

MySQL Deleting

Page 73: Intermediate PHP & MySQL. Welcome This slideshow presentation is designed to introduce you to some intermediate PHP concepts and an introduction to MySQL.

More web workshops can be found atwww.tinyurl.com/rpi123

End of the Workshop


Recommended