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

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement.
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Website Development & Management PHP Odds & Ends Instructor: John Seydel, Ph.D. CIT Fall
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
INTERNET APPLICATION DEVELOPMENT For More visit:
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
Advanced Web 2012 Lecture 4 Sean Costain PHP Sean Costain 2012 What is PHP? PHP is a widely-used general-purpose scripting language that is especially.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
About Dynamic Sites (Front End / Back End Implementations) by Janssen & Associates Affordable Website Solutions for Individuals and Small Businesses.
MySQL + PHP.  Introduction Before you actually start building your database scripts, you must have a database to place information into and read it from.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
PHP TUTORIAL. HISTORY OF PHP  PHP as it's known today is actually the successor to a product named PHP/FI.  Created in 1994 by Rasmus Lerdorf, the very.
PHP meets MySQL.
1 Accelerated Web Development Course JavaScript and Client side programming Day 2 Rich Roth On The Net
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
Prof Frankl, Spring 2008CS Polytechnic University 1 Overview of Web database applications with PHP.
Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Intro to PHP Carl-Erik Svensson. What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
PHP, Databases, and Cookies Dave Pease IDS496 12/2/2003
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
Web Database Programming Using PHP
Introduction to Dynamic Web Programming
Web Database Programming Using PHP
DBW - PHP DBW2017.
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
PHP & MySQL Introduction.
PHP Training at GoLogica in Bangalore
PHP / MySQL Introduction
BASIC PHP and MYSQL Edward S. Flores.
Intro to PHP & Variables
ISC440: Web Programming 2 Server-side Scripting PHP 3
Building Web Applications
PHP.
Tutorial 6 PHP & MySQL Li Xu
PHP an introduction.
PHP-II.
Presentation transcript:

PHP (2) – Functions, Arrays, Databases, and sessions

Location of User defined PHP functions Where can PHP Functions be located ? The short answer is: Anywhere! Before the section. In the... section. In the... section

PHP Function Location

The output of the previous slide

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

Example of external PHP function file

Result of interpreting the php file on the previous page

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.

Example function returning a value

Result of interpreting the code in the previous slide

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.

Creating Indexed Arrays and the scope of loops

The result of accessing the previous page

Creating Associative Arrays

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.

Using each() and reset()

The result of running the previous page.

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.

Using the foreach() function

The result of accessing the previous page

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

PHP and MySQL (2) PHP functions that interact with MySQL may be classified into groups. These are shown in the table below:-

Connecting and Disconnecting to MySQL Databases using PHP

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.

Using the define() function

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.

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.

Performing MySQL queries using PHP

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.

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.

Contents of books table

A form driven query can be constructed to search the table

Searchbooks.php

The result of running the previous slide

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.

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.

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.

Processing the results associative array (2)

Processing the results associative array (3)

PHP functions returning other information about MySQL

Example usage of mysql_list_dbs()

Output of the previous slide

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

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.

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.

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.

Session files structure

PHP Session – setup form

Running setup.php

view.php

Running view.php

Ending a session - unreg.php

 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 id  The 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

 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

References: Textbook (Meloni) The php.net site has a lot of information about sessions and MySQL. See and There is a good discussion of sessions at apter/ch08.html apter/ch08.html In addition, a search on Google will bring up many other websites.