© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University

Slides:



Advertisements
Similar presentations
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Advertisements

© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic PHP.
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
Information Technologies Anselm Spoerri PhD (MIT)
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
© Anselm Spoerri Web Design Information Visualization Course Prof. Anselm Spoerri
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
ALBERT WAVERING BOBBY SENG. Week Whatever: PHP  Announcements/questions/complaints.
CSC 318 WEB APPLICATION DEVELOPMENT.  Introduction to Server Scripting language  Client VS Server  Introduction to PHP  PHP Files and Syntax  Function.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
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.
PHP : Hypertext Preprocessor
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
PHP and MySQL for Client-Server Database Interaction Chapter 10.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
PHP and JavaScript Nov. 26, 2013 Kyung Eun Park Computer and Creativity (COSC109) Towson University.
© Anselm Spoerri Web Design Information Visualization Course Prof. Anselm Spoerri
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
CS 174: Web Programming September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Dynamic web content HTTP and HTML: Berners-Lee’s Basics.
Introduction to PHP Advanced Database System Lab no.1.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
PHP Constructs Advance Database Management Systems Lab no.3.
CSE 154 LECTURE 5: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
© Anselm Spoerri Web Design Information Visualization Course Prof. Anselm Spoerri
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
MySQL. Is a SQL (Structured Query Language) database server. Can be accessed using PHP with embedded SQL Queries Supports Large DB’s, 60,000 tables with.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
CST336, Dr. Krzysztof Pietroszek Week 2: PHP. 1.Introduction to PHP 2.Embed PHP code into an HTML web page 3.Generate (output HTML) web page using PHP.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
Chapter 3 Introduction to PHP. Incorporating PHP Within HTML By default, PHP documents end with the extension.php files ending with.htm or.html to also.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
PHP Form Processing * referenced from
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
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,
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
PHP using MySQL Database for Web Development (part II)
Web Database Programming Using PHP
Web Database Programming Using PHP
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
PHP Introduction.
Intro to PHP & Variables
PHP.
Information Technologies Anselm Spoerri PhD (MIT)
Presentation transcript:

© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University

© Anselm SpoerriInfo + Web Tech Course Lecture 10 - Overview PHP –Overview –$variables –Strings –Arrays –Function –IF test –FOR loop MySQL & PHP –login.php –Connect to Your MySQL Database –Run Query & Process Results Lectures – Week 10 Content

© Anselm SpoerriInfo + Web Tech Course Dynamic Client/Server Request/Response Sequence $query $result

© Anselm SpoerriInfo + Web Tech Course Recap – Dynamic Web 1.JavaScript –Client-side scripting access to elements of HTML document –Perform data handling and web server requests in background 2.PHP –Server-side scripting –Open source, simplicity and built-in links to MySQL database 3.MySQL –Open-source Relational Databases that supports structured queries and is free to use and install on web servers 4.Apache Server –Open source server software that serves up HTML, media files etc

© Anselm SpoerriInfo + Web Tech Course PHP – Overview Open-source : documentation Server-side scripting –Insert content of one file (or the output of a system call) into another file dynamically –Create images and other files for you, either on the fly or in advance to serve up later. –PHP program responsible for passing back clean file suitable for display in web browser. Simplicity & Flexibility Built-in links to MySQL database.php file extension

© Anselm SpoerriInfo + Web Tech Course PHP – Overview (cont.) $ count = 10 ; Must Place $ in front of all Variables Common Error = Forget Semicolon $username = "Fred Smith"; // quotation marks for string echo $username; // echo makes variable visible in Browser

© Anselm SpoerriInfo + Web Tech Course PHP – Code Construction XHTML tags included inside of PHP code (thus have DOCTYPE declaration and basic XHTML structure) Some programmers open tag at start of document and close it at the end, outputting any HTML directly from PHP commands. Others choose to insert only smallest possible fragments of PHP within these tags wherever dynamic scripting is required, leaving the rest of the document in standard HTML. Hello World. Today is. Latter style may produce faster code, while the former has less complexity having to drop in and out of PHP many times in a single document.

© Anselm SpoerriInfo + Web Tech Course PHP - Variable Naming Rules Starts with $ Variable Naming Rules When creating PHP variables, you must follow these four rules: ‒ Must start with a letter of alphabet or _ (underscore) ‒ Can contain only the characters: a-z, A-Z, 0-9, and _ ‒ May not contain spaces. (e.g., $user_name). ‒ Variable names are case-sensitive. The variable $High_Score is not the same as the variable $high_score.

© Anselm SpoerriInfo + Web Tech Course PHP - Strings PHP supports two types of strings: "hello" or 'hello' –Literal string (preserve exact contents), use apostrophe: $info = 'Preface variables with a $ like this: $variable'; –Include value of variable (evaluate string): use quotation marks $count = 44; echo "There have been $count presidents of the US"; Escape Character : \ –$text = 'My friend\'s car is a Chevy'; –$text = "My mother always said \"Eat your greens\"."; –Important to use when HTML code and variables used inside echo statement

© Anselm SpoerriInfo + Web Tech Course PHP – Array Operations $team = array('Bill', 'Joe', 'Mike', 'Chris'); // apostrophes echo $team[3]; // Displays the name Chris  array indexes start at zero $twoDim = array(array('a', '', 'c'), array('', 'b', ''),); echo $twoDim[1][1]; count($a); // number of array items Array union: $a + $b Associative Array –$a['firstname'] = "John";

© Anselm SpoerriInfo + Web Tech Course PHP - Operators Operators ++$j or --$j // Increment or Decrement "FirstName". " ". "LastName" // String concatenation Boolean AND: && AND OR: || OR $firstname && $lastname $firstname || $lastname Equality Operator is == Not Equal is != Identity Operator is === (prevents type conversion) Operators precedence rules  use parentheses to control what gets computed first and in which order

© Anselm SpoerriInfo + Web Tech Course PHP – Constants, Global and Function Define Constant define("ROOT_LOCATION", "/usr/local/www/"); –Can not be prefaced with $ sign and need to use define function –Use UPPERCASE global $username; Define Function function function_name ([parameter [,...]]) { // Statements } function squared ($count) { return $count * $count;} Scope of Variable –Local variables are accessible just from part of code where defined: Defined inside of function, only accessible inside of function. Defined outside of function, only accessible outside of function. –Global variables are accessible from all parts of your code. Forgetting Scope of Variable is Common Error.

© Anselm SpoerriInfo + Web Tech Course PHP – IF test if (test) {do1;} else {do2;} test ? doifTRUE : doifFALSE; if (test) {do1;} elseif (test2) {do2;} else {do3;}

© Anselm SpoerriInfo + Web Tech Course PHP – FOR Loop AND FOREACH Loop for loop for (init variable ; test variable ; increment variable) { do something; } for ($count = 1 ; $count "; } foreach loop used for arrays $paper = array("Copier", "Inkjet", "Laser", "Photo"); $j = 0; foreach ($paper as $item) { echo "$j: $item "; ++$j; }

© Anselm SpoerriInfo + Web Tech Course PHP – Include Code from Other Files include_once "filename"; –Tell PHP to fetch a particular file and load all its contents –To avoid error of trying to define same constant or function multiple times require_once "filename"; –To make sure that file is found

© Anselm SpoerriInfo + Web Tech Course Set Up Connection to PHP & MySQL Server Open Filezilla and select Site created for “studentweb” Host = “studentweb.comminfo.rutgers.edu” Select SFTP and Normal User = “yourNetIDusername” (or yourSCILSNETusername) Password = “yourNetIDpassword” (or yourSCILSNETpassword) Need to specify path to course folder in “Remote Site” slot in Filezilla: /www/studentweb.comminfo.rutgers.edu/htdocs/YourCourseFolder/username Example: YourCourseFolder = class When moving files to server, remember to set permissions 755 URL to test page in Browser: “ Example:

© Anselm SpoerriInfo + Web Tech Course MySQL & PHP – Process Steps The process of using MySQL with PHP: 1.Connect to MySQL. 2.Select the database to use. 3.Build a query string. 4.Perform the query. 5.Retrieve the results and output it to a web page. 6.Repeat Steps 3 to 5 until all desired data retrieved. 7.Disconnect from MySQL (usually done automatically)

© Anselm SpoerriInfo + Web Tech Course MySQL & PHP – login.php for your database Create login_lastname.php file <?php // login_lastname.php make sure to place in personal folder $db_hostname = 'localhost'; $db_database = 'YourCourseFolder_NetIDusername'; $db_username = 'yourMySQLusername'; $db_password = 'yourMySQLpassword'; ?> Upload to Server, Set Permissions, View Source in Browser Now that you have login.php file saved, you can include it in any PHP files that will need to access the database by using the require_once statement.

© Anselm SpoerriInfo + Web Tech Course MySQL & PHP – Connect to Database – Step 1 <?php // connectDatabase.php require_once 'login_lastname.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if ( ! $db_server) die ("Unable to connect to MySQL: ". mysql_error()); mysql_select_db($db_database) or die("Unable to select database: ". mysql_error()); ?>

© Anselm SpoerriInfo + Web Tech Course MySQL & PHP – Query Database – Step 2 Add to connectDatabase.php $query = "SELECT * FROM classics"; $result = mysql_query($query); if (!$result) die ("Database access failed: ". mysql_error());

© Anselm SpoerriInfo + Web Tech Course MySQL & PHP – Display Query Result – Step 3 Add to connectDatabase.php $rows = mysql_num_rows($result); for ($j = 0 ; $j < $rows ; ++$j){ echo 'Author: '. mysql_result($result,$j,'author'). ' '; echo 'Title: '. mysql_result($result,$j,'title'). ' '; echo 'Year: '. mysql_result($result,$j,'year'). ' '; echo 'ISBN: '. mysql_result($result,$j,'isbn'). ' '; } This code is inefficient and slow, because a total of #classics * 3 calls are made to the function mysql_result in order to retrieve all the data.

© Anselm SpoerriInfo + Web Tech Course MySQL & PHP – Display Query Result (Faster) – Step 4 $rows = mysql_num_rows($result); for ($j = 0 ; $j < $rows ; ++$j){ // Modify for loop and fetch row $row = mysql_fetch_row ($result); // Need to consult MySQL table // to identify correct index value for field echo 'Author: '. $row[0]. ' '; echo 'Title: '. $row[1]. ' '; echo 'Year: '. $row[3]. ' '; echo 'ISBN: '. $row[5]. ' '; }

© Anselm SpoerriInfo + Web Tech Course MySQL & PHP – Connect to Database – Steps 1-4 <?php // query.php require_once 'login_lastname.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die("Unable to connect to MySQL: ". mysql_error()); mysql_select_db($db_database) or die("Unable to select database: ". mysql_error()); $query = "SELECT * FROM classics"; $result = mysql_query($query); if (!$result) die ("Database access failed: ". mysql_error()); $rows = mysql_num_rows($result); for ($j = 0 ; $j < $rows ; ++$j){ $row = mysql_fetch_row($result); echo 'Author: '. $row[0]. ' '; echo 'Title: '. $row[1]. ' '; echo 'Year: '. $row[3]. ' '; echo 'ISBN: '. $row[5]. ' '; ?>

© Anselm SpoerriInfo + Web Tech Course Credential Management MySQL Workbench Connect to studentweb.comminfo.rutgers.edu using NetID (or studentweb) credentials Connect to MySQL using MySQL credentials  Can talk to server with MySQL database from your computer SFTP Connect to studentweb.comminfo.rutgers.edu using NetID (or studentweb) credentials  Can upload HTML and PHP pages to server with MySQL database  Set permissions PHP ‒ Create login_yourlastname.php file that stores MySQL credentials so we can access MySQL from inside PHP code