Download presentation
Presentation is loading. Please wait.
Published byBasil Mathews Modified over 9 years ago
1
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 &14 Intro to SQL Programming Techniques & Web DB + PhP
2
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Introduction to SQL Programming Techniques Database applications Host language Java, C/C++/C#, COBOL, or some other programming language Data sublanguage SQL SQL standards Continually evolving Each DBMS vendor may have some variations from standard
3
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Approaches to Database Programming (cont’d.) Using a library of database functions Library of functions available to the host programming language Application programming interface (API) Designing a brand-new language Database programming language designed from scratch First two approaches are more common
4
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Impedance Mismatch Differences between database model and programming language model Binding for each host programming language Specifies for each attribute type the compatible programming language types Cursor or iterator variable Loop over the tuples in a query result
5
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Typical Sequence of Interaction in Database Programming Open a connection to database server Interact with database by submitting queries, updates, and other database commands Terminate or close connection to database
6
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Embedded SQL, Dynamic SQL, and SQLJ Embedded SQL C language SQLJ Java language Programming language called host language
7
Copyright © 2011 Ramez Elmasri and Shamkant Navathe JDBC: SQL Function Calls for Java Programming JDBC Java function libraries Single Java program can connect to several different databases Called data sources accessed by the Java program Class.forName("oracle.jdbc.driver.OracleDriver") Load a JDBC driver explicitly
8
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
9
JDBC: SQL Function Calls for Java Programming Connection object Statement object has two subclasses: PreparedStatement and CallableStatement Question mark ( ? ) symbol Represents a statement parameter Determined at runtime ResultSet object Holds results of query
10
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Chapter 14 Outline A Simple PHP Example Overview of Basic Features of PHP Overview of PHP Database Programming
11
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Web Database Programming Using PHP Techniques for programming dynamic features into Web PHP Open source scripting language Interpreters provided free of charge Available on most computer platforms
12
Copyright © 2011 Ramez Elmasri and Shamkant Navathe A Simple PHP Example PHP Open source general-purpose scripting language Comes installed with the UNIX operating system
13
Copyright © 2011 Ramez Elmasri and Shamkant Navathe A Simple PHP Example (cont’d.) DBMS Bottom-tier database server PHP Middle-tier Web server HTML Client tier
14
Copyright © 2011 Ramez Elmasri and Shamkant Navathe A Simple PHP Example (cont’d.) Client Tier Middle-tier Web Server Database Server
15
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
16
A Simple PHP Example (cont’d.) Example Figure 14.1(a) PHP script stored in: http://www.myserver.com/example/greeting.php <?php PHP start tag ?> PHP end tag Comments: // or /* */
17
Copyright © 2011 Ramez Elmasri and Shamkant Navathe A Simple PHP Example (cont’d.) $_POST Auto-global predefined PHP variable Array that holds all the values entered through form parameters Arrays are dynamic Long text strings Between opening <<<_HTML_ and closing _HTML_;
18
Copyright © 2011 Ramez Elmasri and Shamkant Navathe A Simple PHP Example (cont’d.) PHP variable names Start with $ sign
19
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Overview of Basic Features of PHP Illustrate features of PHP suited for creating dynamic Web pages that contain database access commands
20
Copyright © 2011 Ramez Elmasri and Shamkant Navathe PHP Variables, Data Types, and Programming Constructs PHP variable names Start with $ symbol Can include characters, letters, and underscore character ( _ ) Main ways to express strings and text Single-quoted strings Double-quoted strings Here documents Nowdoc (PHP 5.3.0 and later)
21
Copyright © 2011 Ramez Elmasri and Shamkant Navathe PHP Variables, Data Types, and Programming Constructs (cont’d.) Period (.) symbol String concatenate operator Single-quoted strings Literal strings that contain no PHP program variables, no parsing/escaping except \’, \\ Double-quoted strings and here documents Values from variables need to be interpolated into string
22
Copyright © 2011 Ramez Elmasri and Shamkant Navathe PHP Variables, Data Types, and Programming Constructs (cont’d.) Numeric data types Integers and floating points Programming language constructs For-loops, while-loops, and conditional if- statements Boolean expressions
23
Copyright © 2011 Ramez Elmasri and Shamkant Navathe PHP Variables, Data Types, and Programming Constructs (cont’d.) Comparison operators == (equal), != (not equal), <> (not equal) > (greater than), >= (greater than or equal), < (less than), and <= (less than or equal) ===, !== (equal and not equal without type juggling) Type juggling will convert strings to numbers if possible
24
Copyright © 2011 Ramez Elmasri and Shamkant Navathe PHP Arrays Database query results Two-dimensional arrays First dimension representing rows of a table Second dimension representing columns (attributes) within a row Main types of arrays: Numeric and associative
25
Copyright © 2011 Ramez Elmasri and Shamkant Navathe PHP Arrays (cont’d.) Numeric array Associates a numeric index with each element in the array Indexes are integer numbers Start at zero Grow incrementally Associative array Provides pairs of (key => value) elements
26
Copyright © 2011 Ramez Elmasri and Shamkant Navathe PHP Arrays (cont’d.)
27
Copyright © 2011 Ramez Elmasri and Shamkant Navathe PHP Arrays (cont’d.) Techniques for looping through arrays in PHP Count function Returns current number of elements in array Sort function Sorts array based on element values in it
28
Copyright © 2011 Ramez Elmasri and Shamkant Navathe PHP Functions Functions Define to structure a complex program and to share common sections of code Arguments passed by value Examples to illustrate basic PHP functions Figure 14.4 Figure 14.5
29
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
30
PHP Functions (cont’d.)
31
Copyright © 2011 Ramez Elmasri and Shamkant Navathe PHP Server Variables and Forms Built-in entries $_SERVER auto-global built-in array variable Provides useful information about server where the PHP interpreter is running
32
Copyright © 2011 Ramez Elmasri and Shamkant Navathe PHP Server Variables and Forms (cont’d.) Examples: $_SERVER['SERVER_NAME'] $_SERVER['REMOTE_ADDRESS'] $_SERVER['REMOTE_HOST'] $_SERVER['PATH_INFO'] $_SERVER['QUERY_STRING'] $_SERVER['DOCUMENT_ROOT'] $_POST Provides input values submitted by the user through HTML forms specified in tag
33
Copyright © 2011 Ramez Elmasri and Shamkant Navathe PHP Including Files include “./path/to/file.php”; Attempts to copy the contents of file.php to the specified path No code included if not found, but script continues execution include_once Equivalent to include but checks to see if the file has already been included first If file is present, execution continues
34
Copyright © 2011 Ramez Elmasri and Shamkant Navathe PHP Including Files (cont’d.) require “./path/to/file.php”; Attempts to copy the contents of file.php to the specified path If file is not found, PHP throws an error and execution stops require_once “./path/to/file.php”; Equivalent to require but checks to see if the file has already been included first If file is present, execution continues
35
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Object Oriented PHP Use the class keyword to declare a new class Declare variables private/public/protected Declare a new object with the new keyword Use -> to access functions Eg. $some_object->my_function();
36
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Object Oriented PHP (cont’d.)
37
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Debugging PHP var_dump($var) Prints the content of the variable $var into the output Also prints variable type information Prints out all values in nested arrays/objects Third party libraries Kint prints out formatted info in a tree-like format
38
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Debugging PHP (cont’d.)
39
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Overview of PHP Database Programming PEAR DB library Part of PHP Extension and Application Repository (PEAR) Provides functions for database access
40
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Connecting to a Database Library module DB.php must be loaded DB library functions accessed using DB:: DB::connect('string') Function for connecting to a database Format for 'string' is: :// : @
41
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
42
Connecting to a Database (cont’d.) Query function $d->query takes an SQL command as its string argument Sends query to database server for execution $d–>setErrorHandling(PEAR_ERROR_DIE) Terminate program and print default error messages if any subsequent errors occur
43
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Collecting Data from Forms and Inserting Records 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–>nextID to create a sequence of unique values for a particular table Placeholders Specified by ? symbol
44
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Retrieval Queries from Database Tables $q Query result $q->fetchRow() retrieve next record in query result and control loop $d->getAll Holds all the records in a query result in a single variable called $allresult
45
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
46
Summary PHP scripting language Very popular for Web database programming PHP basics for Web programming Data types Database commands include: Creating tables, inserting new records, and retrieving database records
47
Copyright © 2011 Ramez Elmasri and Shamkant Navathe NEED TO ADD – USING REST APIs SLIDES
48
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Project Phase II Instructions Download XAMPP XAMPP includes: Apache, PHP, MySQL, and other goodies. No need to download and install them separately, XAMPP does it all. Download: https://www.apachefriends.org/download.html https://www.apachefriends.org/download.html FAQs (Includes instructions on how to install it and set it up): Windows FAQs: https://www.apachefriends.org/faq_windows.html https://www.apachefriends.org/faq_windows.html OSX FAQs: https://www.apachefriends.org/faq_osx.htmlhttps://www.apachefriends.org/faq_osx.html Linux FAQs: https://www.apachefriends.org/faq_linux.htmlhttps://www.apachefriends.org/faq_linux.html
49
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Project Phase II Instructions Download Aptana Studio Aptana Studio is a web development IDE rooted on Eclipse. You can use your preferred IDE for the project, it does not need to be Aptana. Download: http://www.aptana.com/products/studio3/download.html http://www.aptana.com/products/studio3/download.html You can download Aptana Studio 2, 3 or either as an Eclipse plugin. For the instructions on this guide, we will use the standalone (not the Eclipse plugin) version of Aptana Studio 3.
50
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Project Phase II Instructions Sample API (Slim Framework) Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. Slim Framework documentation: http://docs.slimframework.com/ http://docs.slimframework.com/ See : TBD Sample GUI Web-App (for CRUD operations) A sample CRUD (Create, Read, Update, Delete) web- app that interfaces with the MySQL database to use. See : TBD
51
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Setting up a Sample DB XAMPP bundles in PhpMyAdmin (http://www.phpmyadmin.net/home_page/index.php)http://www.phpmyadmin.net/home_page/index.php
52
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Set PhpMyAdmin web-app Go to: http://localhost/phpmyadminhttp://localhost/phpmyadmin If you don’t use Port 80 – Use http://localhost:{yourport}/phpmyadmin; where {yourport} is the custom port.
53
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Setting up the Sample API Add sample_api, to you XAMPP’s htdocs folder With sample_api in place: Open Aptana Studio (or your IDE) Click File -> Import Select Existing Folder as New Project Select a folder. Navigate to your C:/xampp/htdocs/sample_api folder Give the project a name Select PHP and Web - Primary as the project types Click on Finish
54
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Sample_gui – customers.php
55
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Sample_gui – customers.php
56
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Sample_api – index.php
57
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Sample_api – index.php
58
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Sample_api – index.php
59
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Sample_api – index.php
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.