MS3304: Week 8 Creating a dynamic SQL query from form input.

Slides:



Advertisements
Similar presentations
Relational Database Systems Higher Information Systems Advanced Implementation in MySQL/PHP.
Advertisements

Relational Database Systems Higher Information Systems Advanced Implementation in Filemaker Pro.
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Mark Dixon Page 1 02 – Queries: Query by Example.
Some Introductory Programming 1. Structured Query Language - used for queries. - a standard database product. 2. Visual Basic for Applications - use of.
Databases Ms. Scales. What is a Database? Database  A collection of data organized for fast search and retrieval  Examples: Telephone Directories Hospital.
MS Access: Database Concepts Instructor: Vicki Weidler.
1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom.
WEEK 11 Database Design. Agenda Hybrid Review Create Tables Add, Edit Data Create Relationships in MS Access 2010 Queries.
MS3304: Week 4 PHP & HTML Forms. Overview HTML Forms elements refresher Sending data to a script via an HTML form –The post vs. get methods –Name value.
1 Insert, Update and Delete Queries. 2 Return to you Address Book database. Insert a record.
DB Implementation: MS Access Queries. MS Access Queries Database Queries Core DBA skill From SQL to Query by Example (QBE) What does it do? Find target.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
 The WHERE clause, also called the predicate, provides the power to narrow down the scope of the data retrieved.  Comparison Operators Comparison OperatorDefinition.
Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
Lesson 31: Querying a Database. 2 Learning Objectives After studying this lesson, you will be able to:  Create, save, and run select queries  Design.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
SQL Review Tonga Institute of Higher Education. SQL Introduction SQL (Structured Query Language) a language that allows a developer to work with data.
PHP meets MySQL.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. ACCESS 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 6 – Designing.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
MS3304: Week 7 Talking to Databases: SQL Basics. Overview Connecting to a database Querying a database Basic SQL syntax for mySQL Writing simple and complex.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Using Special Operators (LIKE and IN)
DAY 19: MICROSOFT ACCESS – CHAPTER 3 CONTD. Aliya Farheen March 17, 2015.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ASP.NET More on searching databases 1ASP.NET, More on searching databases.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
1 Database Systems Introduction to Microsoft Access Part 2.
Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
School of Geography FACULTY OF ENVIRONMENT Querying with ArcGIS.
Access Queries Agenda 6/16/14 Review Access Project Part 1, answer questions Discuss queries: Turning data stored in a database into information for decision.
SQL SELECT Getting Data from the Database. Basic Format SELECT, FROM WHERE (=, >, LIKE, IN) ORDER BY ; SELECT LastName, FirstName, Phone, City FROM Customer.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
DAY 18: MICROSOFT ACCESS – CHAPTER 3 CONTD. Akhila Kondai October 21, 2013.
Access Review. Table: Make your table in design view – like we’ve done for all databases in class.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
Querying Databases A query is a program that allows us to VIEW the data or operate on the data Several types of queries –Select query –Merge query –Summary.
# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105.
Lesson 4: Querying a Database. 2 Learning Objectives After studying this lesson, you will be able to:  Create, save, and run select queries  Set query.
Fall Lab 131 CS105 Lab 13 – Logical Operator Precedence and Joining Tables Announcements: MP 3 released Friday, 11/20 Honors project due: Tuesday,
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
ADVANCED SQL.  The SQL ORDER BY Keyword  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the.
Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
MICROSOFT ACCESS – CHAPTER 3 CONTD. Sravanthi Lakkimsetty Mar 09, 2016.
Creating Functions This presentation was prepared by Professor Steve Ross, with the advice of other MIS Faculty, for use in MIS Classes at Western Washington.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Chapter 5 Introduction to SQL.
Unit M Programming Web Pages with
3.5 Databases Relationships.
Writing Basic SQL SELECT Statements
Basic select statement
Using Subqueries to Solve Queries
IS-171 Computing With Spreadsheets
Using Subqueries to Solve Queries
Text Analyzer BIS1523 – Lecture 14.
Spreadsheets, Modelling & Databases
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Objectives In this lesson, you will learn to:
Presentation transcript:

MS3304: Week 8 Creating a dynamic SQL query from form input

Overview Building an SQL statement dynamically from form input Writing/Testing dynamic code

Review of an SQL statement SELECT which columns FROM which table WHERE conditions ORDER BY fields to sort by

Our student database fields & search form Field NameData type firstNamevarchar surnamevarchar studentNumbervarchar groupint cw1int cw2int averageint bookstinyint librarytinyint clubtinyint cinematinyint altMedtinyint teamvarchar MM_Progtinyint IT_Progtinyint

Field, element and variable names Form Element NameDatabase NamePHP Variable Name firstName $firstName surname $surname studentNumber $studentNumber group $group cw1 $cw1 cw1Op$cw1Op cw2 $cw2 cw2Op$cw2Op cwAverage $cwAverage cwAverageOp$cwAverageOp books, library, club, cinema, altMed project$books, $library, $club, $cinema, $altMed team $team IT_prog $IT_prog MM_prog $MM_prog sortOrder$sortOrder

Comparing string input Need to make sure to trim off any extra spaces using trim() function Need to make sure to unescape any special characters using the stripslashes() function May need to use the wildcards for string comparison

Creating a dynamic SQL string Q: How do you go about creating a dynamic SQL statement? A: Break down the statement into parts! –SELECT –FROM –WHERE –ORDER BY $SQLcmd = $SQLSelectFrom. $SQLWhere. $SQLSort; $SQLSelectFrom = " " ; $SQLWhere = " " ; $SQLSort = " " ;

Using variables in the query string Last week we learned that we could use variable names in our SQL query SELECT * FROM students WHERE group = $group SELECT * FROM students WHERE surname = “$surname” SELECT * FROM students WHERE surname LIKE “$surname%”

The SELECT/FROM string For this exercise we assume that the all fields will be returned The name of the table is students What is the string we need to create? $SQLSelectFrom =

Creating the ORDER BY string Use the value of the Sort by select object to return pass in the field name of the column you wish to sort by What is the code we need to write? $SQLSort =

The WHERE string This statement can be made up of any combination of the criteria listed Since we may have multiple statements all statements need to be to be enclosed in parentheses If we have multiple statements they need to be connected by an AND or an OR operator We need to know the name of the PHP variable so we can get the value and the name of the database field we will be searching

Comparing string input Need to make sure to trim off any extra spaces using trim() function Need to make sure to unescape any special characters using the stripslashes() function May need to use the wildcards for string comparison

Creating the WHERE statement: testing the first criteria Must have conditional statements to check to see whether or not to add a search criteria to the field What conditional statement do we use to check to see if we add firstName to the criteria?

Creating the WHERE statement: testing the second criteria Must have conditional statements to check to see whether or not to add a search criteria to the field What conditional statement do we use to check to see if we add surname to the criteria?

Adding operators for multiple criteria Must use conditional statements to check to see if the statement is the first criteria listed to decide if an AND operator must be added to the string to separate two criteria Where does this test go logically? What is the conditional statement to test for it?

Creating the WHERE statement: your turn Write the conditional statements dynamically build the $SQLWhere statement for the following criteria: –studentNumber –group –cw1 –cw2 –average –books –library –club –cinema –altMed –team –MM_Prog –IT_Prog

Writing dynamic code Plan out the logic behind your code first using comments Write your code in small chunks and test each piece of code that you write to make sure it works before you start the next