Copyright 2016 Ramez Elmasri and Shamkant B Navathe
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
Chapter 11 Outline n n n A Simple PHP Example Overview of Basic Features of PHP Overview of PHP Database Programming Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 2
Web Database Programming Using PHP n n Techniques for programming dynamic features into Web PHP n n n Open source scripting language Interpreters provided free of charge Available on most computer platforms Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 3
A Simple PHP Example n PHP n n Open source general-purpose scripting language Comes installed with the UNIX operating system Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 4
A Simple PHP Example (cont’d. ) n DBMS n n PHP n n Bottom-tier database server Middle-tier Web server HTML n Client tier Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 5
Figure 11. 1 a PHP program segment for entering a greeting. continued on next slide Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 6
Figure 11. 1 b-d (b) Initial form displayed by PHP program segment. (c) User enters name John Smith. (d) Form prints welcome message for John Smith. Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 7
A Simple PHP Example (cont’d. ) n n Example Figure 11. 1(a) PHP script stored in: n n <? php n n PHP start tag ? > n n http: //www. myserver. com/example/greeting. php PHP end tag Comments: // or /* */ Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 8
A Simple PHP Example (cont’d. ) n $_POST n n Auto-global predefined PHP variable Array that holds all the values entered through form parameters Arrays are dynamic Long text strings n Between opening <<<_HTML_ and closing _HTML_; Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 9
A Simple PHP Example (cont’d. ) n PHP variable names n Start with $ sign Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 10
Overview of Basic Features of PHP n Illustrate features of PHP suited for creating dynamic Web pages that contain database access commands Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 11
PHP Variables, Data Types, and Programming Constructs n PHP variable names n n n Start with $ symbol Can include characters, letters, and underscore character (_) Main ways to express strings and text n n Single-quoted strings Double-quoted strings Here documents Single and double quotes Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 12
PHP Variables, Data Types, and Programming Constructs (cont’d. ) n Period (. ) symbol n n Single-quoted strings n n String concatenate operator Literal strings that contain no PHP program variables Double-quoted strings and here documents n Values from variables need to be interpolated into string Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 13
PHP Variables, Data Types, and Programming Constructs (cont’d. ) n Numeric data types n n Programming language constructs n n Integers and floating points For-loops, while-loops, and conditional ifstatements Boolean expressions Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 14
Figure 11. 2 Illustrating basic PHP string and text values. Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 15
PHP Variables, Data Types, and Programming Constructs (cont’d. ) n Comparison operators n == (equal), != (not equal), > (greater than), >= (greater than or equal), < (less than), and <= (less than or equal) Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 16
PHP Arrays n Can hold database query results n n Two-dimensional arrays First dimension representing rows of a table Second dimension representing columns (attributes) within a row Main types of arrays: n Numeric and associative Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 17
PHP Arrays (cont’d. ) n Numeric array n n Associates a numeric index with each element in the array Indexes are integer numbers n n n Start at zero Grow incrementally Associative array n Provides pairs of (key => value) elements Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 18
Figure 11. 3 Illustrating basic PHP array processing. Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 19
PHP Arrays (cont’d. ) n n Techniques for looping through arrays in PHP Count function n n Returns current number of elements in array Sort function n Sorts array based on element values in it Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 20
PHP Functions n n n Define to structure a complex program and to share common sections of code Arguments passed by value Examples to illustrate basic PHP functions n n Figure 11. 4 Figure 11. 5 Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 21
Figure 11. 4 Rewriting program segment P 1 as P 1′ using functions. Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 22
Figure 11. 5 Illustrating a function with arguments and return value. Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 23
PHP Server Variables and Forms n Built-in entries n n $_SERVER auto-global built-in array variable Provides useful information about server where the PHP interpreter is running Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 24
PHP Server Variables and Forms (cont’d. ) n Examples: n n n n $_SERVER['SERVER_NAME'] $_SERVER['REMOTE_ADDRESS'] $_SERVER['REMOTE_HOST'] $_SERVER['PATH_INFO'] $_SERVER['QUERY_STRING'] $_SERVER['DOCUMENT_ROOT'] $_POST n Provides input values submitted by the user through HTML forms specified in <INPUT> tag Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 25
Overview of PHP Database Programming n PEAR DB library n n Part of PHP Extension and Application Repository (PEAR) Provides functions for database access Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 26
Connecting to a Database n n n Library module DB. php must be loaded DB library functions accessed using DB: : <function_name> DB: : connect('string') n n Function for connecting to a database Format for 'string' is: <DBMS software>: //<user account>: <password>@<database server> Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 27
Figure 11. 6 Connecting to a database, creating a table, and inserting a record. Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 28
Some Database Functions n Query function n $d->query takes an SQL command as its string argument Sends query to database server for execution $d–>set. Error. Handling(PEAR_ERROR_DIE) n Terminate program and print default error messages if any subsequent errors occur Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 29
Collecting Data from Forms and Inserting Records n n Collect information through HTML or other types of Web forms Create unique record identifier for each new record inserted into the database PHP has a function $d–>next. ID to create a sequence of unique values for a particular table Placeholders n Specified by ? symbol Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 30
Retrieval Queries from Database Tables n $q n n n Variable that holds query result $q->fetch. Row() retrieve next record in query result and control loop $allresult = $d->get. All(query) n Holds all the records in a query result in a single variable called $allresult Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 31
Figure 11. 7 Illustrating database retrieval queries. Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 32
Other techniques n PHP runs on server n n Sends HTML to client Many other languages/technologies for Web Db programming Examples: Java servlets: n n Java objects on server, interact with client Store information about interaction session Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 33
Other techniques (cont. ) n Java Server Pages (JSP) n n Java. Script n n Creates dynamic Web pages through scripting at server to send to client (somewhat like PHP) Scripting language, can run at client or server Java Script Object Notation (JSON): n n n Text-based representation of objects Similar function to XML Used in many NOSQL systems Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 34
Summary n PHP scripting language n n Very popular for Web database programming PHP basics for Web programming Data types Database commands include: n n Creating tables, inserting new records, and retrieving database records Looping over a query result Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 11 - 35
- Slides: 35