Top Banner
PHP (2) – Functions, Arrays, Databases, email and sessions
58
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PHP (2) – Functions, Arrays, Databases, email and sessions.

PHP (2) – Functions, Arrays,

Databases, email and sessions

Page 2: PHP (2) – Functions, Arrays, Databases, email and sessions.

Location of User defined PHP functions

Where can PHP Functions be located ?

The short answer is: Anywhere!

Before the <head> section. In the <head> ... </head> section. In the <body> ... </body> section

Page 3: PHP (2) – Functions, Arrays, Databases, email and sessions.

PHP Functio

nLocatio

n

Page 4: PHP (2) – Functions, Arrays, Databases, email and sessions.

The output of the previous slide

Page 5: PHP (2) – Functions, Arrays, Databases, email and sessions.

External PHP Functions Can we put PHP functions in external

files and load them into pages ? - Yes !

When a website gets beyond a few pages in size, then it is most convenient to put the PHP functions in one or more external files. This also facilitates the maintenance and upgrading of the site

Page 6: PHP (2) – Functions, Arrays, Databases, email and sessions.

Example of external PHP function file

Page 7: PHP (2) – Functions, Arrays, Databases, email and sessions.

Result of interpreting the php file on the previous page

Page 8: PHP (2) – Functions, Arrays, Databases, email and sessions.

Returning Values from functions

PHP functions can return a value – note that returning a value from a function is optional.

The syntax for returning a single value is fairly straightforward – you just use the return keyword followed by the variable or value you want to return from the function.

PHP functions can also return nothing at all. i.e a function doesn’t have to use the return keyword at all or it can use return and not have a variable or value after it.

Page 9: PHP (2) – Functions, Arrays, Databases, email and sessions.

Example function returning a value

Page 10: PHP (2) – Functions, Arrays, Databases, email and sessions.

Result of interpreting the code in the previous slide

Page 11: PHP (2) – Functions, Arrays, Databases, email and sessions.

PHP Arrays An array in PHP is a list of key/value elements

in which any number of values can be stored (provided there is enough memory available).

There are 2 types of arrays in PHP – indexed and associative

Indexed arrays are the ones most people are familiar with. The key (or index) to each value is an integer.

Associative arrays can be useful at times. Members of associative arrays are accessed by means of text strings. The key (or index) to each value is a string.

When working with arrays in PHP, there is frequently more than one way of accomplishing a task.

Page 12: PHP (2) – Functions, Arrays, Databases, email and sessions.

Creating

Indexed

Arraysand the scope

of loops

Page 13: PHP (2) – Functions, Arrays, Databases, email and sessions.

The result of accessing the previous page

Page 14: PHP (2) – Functions, Arrays, Databases, email and sessions.

Creating Associative Arrays

Page 15: PHP (2) – Functions, Arrays, Databases, email and sessions.

Accessing the elements of an Associative Array

Elements can be accessed easily by means of their key.

If you want to access each element in sequence there are a number of approaches.

One simple approach is to use the each() function.

Page 16: PHP (2) – Functions, Arrays, Databases, email and sessions.

Using each() and reset()

Page 17: PHP (2) – Functions, Arrays, Databases, email and sessions.

The result of running the previous page.

Page 18: PHP (2) – Functions, Arrays, Databases, email and sessions.

The foreach() function This is similar to the each() function, but

it incorporates a loop and traverses the whole array it is applied to.

The foreach() function takes two arguments.

The first argument is the name of the array to be traversed. The second argument is the name of the variable to hold the individual element.

The foreach() function can be applied to multidimensional arrays and associative arrays as well as indexed arrays.

Page 19: PHP (2) – Functions, Arrays, Databases, email and sessions.

Using the foreach() function

Page 20: PHP (2) – Functions, Arrays, Databases, email and sessions.

The result of accessing the previous page

Page 21: PHP (2) – Functions, Arrays, Databases, email and sessions.

PHP functions supporting MYSQL operations

PHP has a large number of functions that enable it to interact with mysql databases.

This includes functions to open and close connections with databases, pass SQL queries to MYSQL tables, and process the results of queries.

The results of MYSQL queries in PHP can usually be treated as an array

Page 22: PHP (2) – Functions, Arrays, Databases, email and sessions.

PHP and MySQL (2) PHP functions that interact with MySQL may

be classified into groups. These are shown in the table below:-

Page 23: PHP (2) – Functions, Arrays, Databases, email and sessions.

Connecting and Disconnecting to MySQL

Databases using PHP

Page 24: PHP (2) – Functions, Arrays, Databases, email and sessions.

Define a PHP constant by using the define() function

The PHP define() function enables PHP programmers to create named data items whose value cannot be changed.

Once a constant is defined, it can never be changed or undefined in the script.

Page 25: PHP (2) – Functions, Arrays, Databases, email and sessions.

Using the define() function

Page 26: PHP (2) – Functions, Arrays, Databases, email and sessions.

Creating and dropping databases in PHP

There are 2 functions in this group. These are mysql_create_db and mysql_drop_db.

These functions would normally only be used by the administrator of an e-commerce website.

The power to create and destroy tables is not something that outside users of e-commerce sites should be allowed to do. There are very severe security considerations associated with these commands.

Page 27: PHP (2) – Functions, Arrays, Databases, email and sessions.

MySQL Access Privileges In MySQL (and most other serious

database systems) it is possible to set different access levels for different users.

Access privileges for MySQl include select (i.e. query), insert, update, delete, create, drop, reload and many others.

Casual users of database systems (e.g. the user “potiro” in the poti database system) are normally only given the “select” privilege. This means that they can only read tables using the “select” statement.

Page 28: PHP (2) – Functions, Arrays, Databases, email and sessions.

Performing MySQL queries using PHP

Page 29: PHP (2) – Functions, Arrays, Databases, email and sessions.

MySQL Queries As outlined last week, MySQL groups data

into databases. Each database consists of one or more tables. It is the tables that actually contain useful data.

An example database is the poti database on the MySQL server on sally.

The user “potiro” has access to the mysql server on sally.

Rather than having unrestricted access to all databases on the mysql server on sally, the user potiro only has access to the database poti. This access is further restricted to read access only. That is, the user potiro can only read tables in the database poti.

Page 30: PHP (2) – Functions, Arrays, Databases, email and sessions.

MySQL Select Query example

Consider a table called “books” in a database called “wea”. The table has the structure outlined below:-

To run a query that selects all columns of all rows in the table books that have the author_lastname column equal to “Barrow” we can either run a select command interactively or use the PHP mysql_query command.

Page 31: PHP (2) – Functions, Arrays, Databases, email and sessions.

Contents of books table

Page 32: PHP (2) – Functions, Arrays, Databases, email and sessions.

A form driven query can be constructed to search the table

Page 33: PHP (2) – Functions, Arrays, Databases, email and sessions.

Searchbooks.php

Page 34: PHP (2) – Functions, Arrays, Databases, email and sessions.

The result of running the previous slide

Page 35: PHP (2) – Functions, Arrays, Databases, email and sessions.

Accessing the array $a_row as an indexed

array

We can also treat each row of the output of the mysql query as an indexed array.

Page 36: PHP (2) – Functions, Arrays, Databases, email and sessions.

The best alternative for handling the output – an associative array

As shown on the previous slides, processing the result of mysql_query using an indexed array works quite well.

However, processing the result of the query as an associative array gives us a lot more flexibility.

Page 37: PHP (2) – Functions, Arrays, Databases, email and sessions.

Processing the results associative array

If we treat the results of mysql_query as an associative array, we actually have a lot more freedom with what we can do.

For example, we can alter the order in which the columns are printed out. We can also ignore some columns if we wish.

Page 38: PHP (2) – Functions, Arrays, Databases, email and sessions.

Processing the resultsassociative array (2)

Page 39: PHP (2) – Functions, Arrays, Databases, email and sessions.

Processing the resultsassociative array (3)

Page 40: PHP (2) – Functions, Arrays, Databases, email and sessions.

PHP functions returning other information about MySQL

Page 41: PHP (2) – Functions, Arrays, Databases, email and sessions.

Example usage of mysql_list_dbs()

Page 42: PHP (2) – Functions, Arrays, Databases, email and sessions.

Output of the previous slide

Page 43: PHP (2) – Functions, Arrays, Databases, email and sessions.

Sending email from PHP PHP can send email. The function to do this is called mail()

Page 44: PHP (2) – Functions, Arrays, Databases, email and sessions.

Understanding Sessions To use sessions we need to bring

together a number of concepts that we have already looked at in this course.

These are : Forms – getting and posting data

Arrays : Associative and indexed Server $_GET,$_POST and $_REQUEST

arrays, also the $_SESSION array.

Page 45: PHP (2) – Functions, Arrays, Databases, email and sessions.

PHP Sessions PHP Sessions

are a way of storing persistent data, created in one web page, on the server so it can be accessed by multiple web pages.

Page 46: PHP (2) – Functions, Arrays, Databases, email and sessions.

Example Session The next few pages show some example

php and HTML for creating a session and storing two variables “products[]” (an array) and “pos” an integer giving the index number of the next available position in the array.

All 3 pages (setup.php, view.php and unreg.php) begin with a call to session_start()

setup.php is used to create the session initially, view.php is used to view the stored data and unreg.php is used to end the session and destroy any stored data.

Page 47: PHP (2) – Functions, Arrays, Databases, email and sessions.

Session files structure

Page 48: PHP (2) – Functions, Arrays, Databases, email and sessions.
Page 49: PHP (2) – Functions, Arrays, Databases, email and sessions.
Page 50: PHP (2) – Functions, Arrays, Databases, email and sessions.

PHP Session – setup form

Page 51: PHP (2) – Functions, Arrays, Databases, email and sessions.

Running setup.php

Page 52: PHP (2) – Functions, Arrays, Databases, email and sessions.

Running setup.php

Page 53: PHP (2) – Functions, Arrays, Databases, email and sessions.

view.php

Page 54: PHP (2) – Functions, Arrays, Databases, email and sessions.

Running view.php

Page 55: PHP (2) – Functions, Arrays, Databases, email and sessions.

Ending a session - unreg.php

Page 56: PHP (2) – Functions, Arrays, Databases, email and sessions.

The session data is stored in a file that can only be read by the user running the server (at least on Unix systems this is the case)Every session has a unique session idThe stored session data is stored in a special format inside the file.When the session_start() function is called the file is created but is empty.Calling session_unreg() deletes the session file

Some more facts about sessions

Page 57: PHP (2) – Functions, Arrays, Databases, email and sessions.

Sessions normally use cookies to store the session id on the client side.The location of the session files is set by the PHP configuration file (php.ini) which uses the directive session.save_path to set it.

Yet more facts about Sessions

Page 58: PHP (2) – Functions, Arrays, Databases, email and sessions.

References:

Textbook (Meloni) The php.net site has a lot of information

about sessions and MySQL . See http://au2.php.net/session and http://au2.php.net/mysql

There is a good discussion of sessions at http://www.oreilly.com/catalog/webdbapps/chapter/ch08.html

In addition, a search on Google will bring up many other websites.