More loops. Control structures / flow control. while loops. Loops / Iteration / doing things over and over and over and over...

Similar documents
PHP. How Web Applications interact with server side databases CRUD. Connecting and using mysql from PHP PHP provides many mysql specific functions

Server side scripting and databases

IELM 511 Information Systems Design Labs 5 and 6. DB creation and Population

PHP Development - Introduction

PHP: Cookies, Sessions, Databases. CS174. Chris Pollett. Sep 24, 2008.

Chapter 6 Part2: Manipulating MySQL Databases with PHP

Chapter. Accessing MySQL Databases Using PHP

SQL stands for Structured Query Language. SQL lets you access and manipulate databases

What is MySQL? [Document provides the fundamental operations of PHP-MySQL connectivity]

Create Basic Databases and Integrate with a Website Lesson 3

PHP Introduction. Some info on MySQL which we will cover in the next workshop...

Lecture 13: MySQL and PHP. Monday, March 26, 2018

Lecture 7: Web hacking 3, SQL injection, Xpath injection, Server side template injection, File inclusion

Database connection 1

Princess Nourah bint Abdulrahman University. Computer Sciences Department

An introduction for the novice. David Lawrence, JLab. 5/8/07 MySQL David Lawrence 1/36

Read this before starting!

CHAPTER 10. Connecting to Databases within PHP

By the end of this section of the practical, the students should be able to:

Database Connectivity using PHP Some Points to Remember:

Announcements. PS 3 is out (see the usual place on the course web) Be sure to read my notes carefully Also read. Take a break around 10:15am

Hello everyone! Page 1. Your folder should look like this. To start with Run your XAMPP app and start your Apache and MySQL.

COM1004 Web and Internet Technology

WEB PROGRAMMING SCV1223. PHP : Authentication Example. Dr. Md Sah bin Hj Salam En. Jumail bin Taliba

Apache, Php, MySql Configuration

PHP. M hiwa ahamad aziz Raparin univercity. 1 Web Design: Lecturer ( m hiwa ahmad aziz)

UNIT V ESTABLISHING A DATABASE CONNECTION AND WORKING WITH DATABASE

Databases and SQL. Lecture outline. CSE 190 M (Web Programming) Spring 2008 University of Washington

MYSQL DATABASE ACCESS WITH PHP

Development Technologies. Agenda: phpmyadmin 2/20/2016. phpmyadmin MySQLi. Before you can put your data into a table, that table should exist.

Jackson State University Department of Computer Science CSC / Advanced Information Security Spring 2013 Lab Project # 3

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

Unit 27 Web Server Scripting Extended Diploma in ICT

PHP: Databases and Classes. CS174. Chris Pollett. Sep 29, 2008.

Comparison with MySQL

A QUICK GUIDE TO PROGRAMMING FOR THE WEB. ssh (then type your UBIT password when prompted)

Advanced Web Tutorial 10

COMP519: Web Programming Autumn 2015

Web Programming. Dr Walid M. Aly. Lecture 10 PHP. lec10. Web Programming CS433/CS614 22:32. Dr Walid M. Aly

Web Application Development (WAD) V th Sem BBAITM (Unit 4) By: Binit Patel

PHP. control structures - loops indexed arrays. Recap: PHP files are processed top to bottom in sequence. Starting at the top

Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

Database-Aware Fault Localization for Dynamic Web Applications

Read this before starting!

The PHP language. Teaching you everything about PHP? Not exactly Goal: teach you how to interact with a database via web

Ampliación de Bases de Datos

PHP: Hypertext Preprocessor. A tutorial Introduction

the Data Drive IN THIS CHAPTER Good Things Come in Free Packages

PHP Arrays. Lecture 20. Robb T. Koether. Hampden-Sydney College. Wed, Feb 28, 2018

Week 11 ~ Chapter 8 MySQL Command Line. PHP and MySQL CIS 86 Mission College

Networks and Web for Health Informatics (HINF 6220)

Lab 7 Introduction to MySQL

Things to note: Each week Xampp will need to be installed. Xampp is Windows software, similar software is available for Mac, called Mamp.

PHP Tutorial 6(a) Using PHP with MySQL

Download and Installation Instructions: After WAMP Server download start the installation:

Simple SCORM LMS Adapter Full Documentation

LISUG web site project

CPSC 421 Database Management Systems. Lecture 10: Embedded SQL

A1 (Part 2): Injection SQL Injection

WEB SECURITY WORKSHOP TEXSAW Presented by Solomon Boyd and Jiayang Wang

Mount Saint Mary College, Newburgh, NY Internet Programming III - CIT310

Instructor s Notes Web Data Management Web Client/Server Concepts. Web Data Management Web Client/Server Concepts

Relational databases and SQL

Introduction to relational databases and MySQL

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

Adding A PHP+MySQL Hit Counter to your Website

Overview of MySQL Structure and Syntax [2]

Using PHP with MYSQL

Networks and Web for Health Informatics (HINF 6220) Tutorial 13 : PHP 29 Oct 2015

School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University

If you do not specify any custom parameters, we will deliver the message using the default names.

PHP and MySQL Programming

Databases. Web applications in PHP to query a database. Practice n. 4

Use of PHP for DB Connection. Middle and Information Tier. Middle and Information Tier

Developing Online Databases and Serving Biological Research Data

Databases - 5. Problems with the relational model Functions and sub-queries

Although this code is under construction the interfaces are unlikely to change, if only because we use it in production.

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

Web Server Setup Guide

Chapters 10 & 11 PHP AND MYSQL

Chapter 1. Introducing PHP COPYRIGHTED MATERIAL

HighPoint Web Based Installation Guide

Building an E-mini Trading System Using PHP and Advanced MySQL Queries ( ) - Contributed by Codex-M

MySQL: Access Via PHP

Database Management Systems by Hanh Pham GOALS

Database Foundations. 6-1 Introduction to Oracle Application Express. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Web Security. Attacks on Servers 11/6/2017 1

How to use SQL to create a database

Lecture 5. Monday, September 15, 2014

CGS 3066: Spring 2017 SQL Reference

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

PHP Reference. To access MySQL manually, run the following command on the machine, called Sources, where MySQL and PhP have been installed:

INSTALLING RACHEL ON SYNOLOGY GIAKONDA IT

R. C. TECHNICAL INSTITUTE, AHMEDABAD COMPUTER ENGG. DEPARTMENT ASSIGNMENT 1

Experimental Data Acquisition and Visualization

DATABASE SYSTEMS. Database programming in a web environment. Database System Course, 2016

CSC 405 Computer Security. Web Security

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Use of PHP for DB Connection. Middle and Information Tier

Transcription:

Control structures / flow control More loops while loops if... else Switch for loops while... do.. do... while... Much of this material is explained in PHP programming 2nd Ed. Chap 2 Control structures / flow control Loops / Iteration / doing things over and over and over and over... if... else Switch for loops while... do.. do... while... } Done these Three standard loop types for while... do... while Much of this material is explained in PHP programming 2nd Ed. Chap 2

Loops / Iteration / doing things over and over and over and over... Loops / Iteration / doing things over and over and over and over... Three standard loop types Three standard loop types for while... do... while Choose a for-loop if the number of times the loop will run is known in advance for while... do... while...or you are processing an array The loop will run 4 times The loop will run 1000 times The loop will run n times Loops / Iteration / doing things over and over and over and over... Loops / Iteration / doing things over and over and over and over... Three standard loop types Three standard loop types for while... do... while Choose a while-loop if the loop will run 0 or more times till some condition becomes false for while... do... while Choose a do-while-loop if the loop will run 1 or more times till some condition becomes false

Loops / Iteration / doing things over and over and over and over... Loops / Iteration / doing things over and over and over and over... Three standard loop types Three standard loop types for while... do... while With these we typically don t know in advance how many times the loop will execute... for while... do... while..but we must know the minimum number of times the loop will work to choose between while or do... while i.e. 0 or 1 while loop The structure of a while statement is: while (condition) statement or with many statements - while (condition){ statement; statement; statement; statement; }; Loop continues whilst condition is true Do something in here to change the condition (unless you want it to continue ) while loop $today="monday"; while($today<>"friday"){ print "<p>today is ".$today."</p>"; if (rand(1,10)>7){ $today="friday"; }; }; print "Final value of today is ".$today; If the condition is initially false, the loop doesn t execute at all (i.e. 0 times)

while loop $today="friday"; while($today<>"friday"){ print "<p>today is ".$today."</p>"; if (rand(1,10)>7){ $today="friday"; }; }; print "Final value of today is ".$today; If the condition is initially false, the loop doesn t execute at all (i.e. 0 times) do.. while loop The structure of a do.. while statement is: do statement; while (condition) or with many statements - do { statement; statement; statement; statement; } while (condition); Will loop at least once Do something in here to change the condition (unless you want it to continue ) do.. while loop do { $i=rand(1,10); print "<p>this time i had the value $i</p>"; } while ($i<8); Server side scripting and databases How Web Applications interact with server side databases As condition is at the bottom of the structure the loop must execute at least once

Three components used in typical web application Web ser ver Browser Web server These can be on different machines in physically different places Database server Database ser ver mysql - http://www.mysql.com/ Web ser ver Apache Database ser ver mysql

mysql Open Source database, issued under a dual commercial license as well mysql - local Start the database process in the background Rather than just being one program, actually consists of many separate components... if doing this on your own machine... Aim to download a complete pack containing apache, PHP and mysql (XAMPP pc or MAMP mac) mysql - dedicated server Use whatever interface is supplied to create a DB mysql - dedicated server Use whatever interface is supplied to create a DB studentnet.kingston.ac.uk

mysql - dedicated server Use whatever interface is supplied to create a DB mysql - dedicated server Use whatever interface is supplied to create a DB mysql - dedicated server Use whatever interface is supplied to create a DB mysql - dedicated server Use whatever interface is supplied to create a DB Keep a note of these values Keep a note of these values DON T use your normal password

phpmyadmin Use a web interface to setup DBs, tables phpmyadmin Use a web interface to setup DBs, tables phpmyadmin - database setup at kingston One database is created by the setup process Example table student db_kxxxxxxxxx student kuid lastname money char char int If using your own machine On your own machine create a database Call this database studentdb

To create a table Click on structure, add name and number of fields To create a table Add fields... Alternative - creating a table using the DDL/SQL Alternative - creating a table using the DDL Syntax create table tablename ( fieldname type, fieldname type,...); Here used to create a table called student create table student(kuid char(255), last name char(255), money int); kuid char student lastname char money int

sql - inserting records using the DML Syntax insert into table [(columnname, columnname, )] values (value, value, ) Here used to insert a record into student insert into student (kuid,lastname,money) values ("ku1200012", "SMITH", 45); sql - querying the database using the DQL Syntax select * or expression from relations [where expression] Here used to show all rows in student select * from student; +-------+----------+-------+ kuid lastname money +-------+----------+-------+ 120 SMITH 45 +-------+----------+-------+ 1 row in set (0.00 sec) sql - running many SQL lines SQL statements separated by semicolons Three components used in typical web application Browser Web server insert into student (kuid,lastname,money) values ("k1200012", "SMITH", 45); insert into student (kuid,lastname,money) values ("k1243012", "AVERY", 22); insert into student (kuid,lastname,money) values ("k1230012", "MITCHELL", 26); insert into student (kuid,lastname,money) values ("k1034512", "WEBB", 22); insert into student (kuid,lastname,money) values ("k1000012", "TENNENT", 22); insert into student (kuid,lastname,money) values ("k1219912", "RUSSELL", 10); Database server

How many items in stock? HTTP request Web server executes code Web server executes code Queries database server

HTML generated Result sent back HTTP response Answer displayed

Connecting and using mysql from PHP PHP provides many mysql specific functions Opening a connection to a mysql server Use mysql_connect mysql_connect mysql_select_db mysql_query mysql_fetch_array Open a link/connection to a mysql database Choose a specific database on a mysql server run an SQL statement on an opened database process a result set mysql_connect (PHP 4, PHP 5) Open a connection to a MySQL Server Description resource mysql_connect ( [string $server [, string $username [, string $password [, bool $new_link [, int $client_flags]]]]] ) Opens or reuses a connection to a MySQL server. mysql_close Close a mysql connection Opening a connection to a mysql server Use mysql_connect <?php // we connect to example.com and port 3307 $link = mysql_connect('example.com:3307', 'user5', 'qwerty5'); if ($link) { die('could not connect: '. mysql_error()); } echo 'Connected successfully'; mysql_close($link); Opens a connection to the mysql server on example.com:3307, using user5 with password qwerty5 Kingston mysql version To connect to the Kingston Uni. mysql database <?php // Create connection $link = mysql_connect('studentnet.kingston.ac.uk:3306', 'kxxxx', 'password'); if ($link) { die('could not connect: '. mysql_error()); } echo 'Connected successfully'; mysql_close($link);?>

MAMP - Local version To connect to a local mysql database - mamp XAMPP - Local version To connect to a local mysql database - xampp <?php // Create connection $link = mysql_connect('localhost:8889', 'root', 'root'); if ($link) { die('could not connect: '. mysql_error()); } echo 'Connected successfully'; mysql_close($link);?> <?php // Create connection $link = mysql_connect('localhost', 'root', ''); if ($link) { die('could not connect: '. mysql_error()); } echo 'Connected successfully'; mysql_close($link);?> Kingston version This example will fail to connect... Select a particular database on a mysql server Use mysql_select_db <?php // Create connection $link = mysql_connect('studentnet.kingston.ac.uk:3306', 'kxxxx', 'missing'); if ($link) { die('could not connect: '. mysql_error()); } echo 'Connected successfully'; mysql_close($link);?> mysql_select_db (PHP 4, PHP 5) Select a MySQL database Description bool mysql_select_db ( string $database_name [, resource $link_identifier] ) Sets the current active database on the server that's associated with the specified link identifier. Every subsequent call to mysql_query() will be made on the active database. $link is only really used for testing the connection and for closing the connection - $link is of type resource - a built in PHP type for this kind of connection Opens a distinct connection to a particular named database on the previously opened mysql server

Select a particular database on a mysql server Use mysql_select_db <?php // we connect to example.com and port 3307 $link = mysql_connect('example.com:3307', 'user5', 'qwerty5'); if ($link) { die('could not connect: '. mysql_error()); } echo 'Connected successfully'; $db_selected = mysql_select_db('foo', $link); if ($db_selected) { die ('Cannot use foo : '. mysql_error()); }; mysql_close($link); Opens a connection to the database foo, using the $link resource Kingston mysql version To open your db_kxxxxxxx database <?php // we connect to localhost $link = mysql_connect('studentnet.kingston.ac.uk:3306', 'kxxxx', 'password'); if ($link) { die('could not connect: '. mysql_error()); } echo 'Connected successfully'; $db_selected = mysql_select_db('db_kxxxxxxx', $link); if ($db_selected) { die ('Cannot use database : '. mysql_error()); } else { print Opened database correctly ; }; mysql_close($link);?> MAMP mysql version To open your studentdb database <?php // we connect to localhost $link = mysql_connect('localhost:8889', 'root', 'root'); if ($link) { die('could not connect: '. mysql_error()); } echo 'Connected successfully'; $db_selected = mysql_select_db('studentdb', $link); if ($db_selected) { die ('Cannot use database : '. mysql_error()); } else { print Opened database correctly ; }; mysql_close($link);?> XAMPP mysql version To open your studentdb database <?php // we connect to localhost $link = mysql_connect('localhost', 'root', ''); if ($link) { die('could not connect: '. mysql_error()); } echo 'Connected successfully'; $db_selected = mysql_select_db('studentdb', $link); if ($db_selected) { die ('Cannot use database : '. mysql_error()); } else { print Opened database correctly ; }; mysql_close($link);?>

Running a SQL statement against the database Use mysql_query mysql_query (PHP 4, PHP 5) Send a MySQL query Description resource mysql_query ( string $query [, resource $link_identifier] ) mysql_query() sends an unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier. Runs an SQL statement against the opened DB Running a SQL statement against the database Use mysql_query $result = mysql_query('insert into foo (field1, field2, field3) values (120, 2000, 22000)'); if ($result) { die('invalid query: '. mysql_error()); } Runs the SQL query - if DDL or DML the result will indicate whether the query ran successfully or not (bool) Version for KU, MAMP and XAMPP Use mysql_query $result = mysql_query('insert into student (kuid, lastname, money) values ( ku123456, HARRIS, 10)'); if ($result) { die('invalid query: '. mysql_error()); } This inserts a single record (or fails) inserting, deleting and updating SQL for inserting, deleting and updating insert into student (kuid, lastname, money) values ( ku123456, HARRIS, 10 ); delete from student where kuid= xxxxx ; update student set lastname= JONES where lastname= xxx ; update student set money=money*2 where lastname= xxx ; Try some out - ie create lines like $result = mysql_query('insert into student (kuid, lastname, money) values ( ku123456, HARRIS, 10)');

To close a database connection Use mysql_close mysql_close (PHP 4, PHP 5) Close MySQL connection Description bool mysql_close ( [resource $link_identifier] ) mysql_close() closes the non-persistent connection to the MySQL server that's associated with the specified link identifier. If link_identifier isn't specified, the last opened link is used. Closes the connection and releases the resources Local version To close the database connection <?php // we connect to the server $link = mysql_connect(.. etc);... mysql_close($link);?>