PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

Similar documents
PHP Queries and HTML Forms Lecture 23

Insertions, Deletions, and Updates

Relational Databases

Views. Lecture 15. Robb T. Koether. Fri, Feb 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, / 28

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

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

The Critical-Path Algorithm

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

Minimal Spanning Trees

XQuery FLOWR Expressions Lecture 35

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

Scope and Parameter Passing

LR Parsing - Conflicts

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

The Class Construct Part 1

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

The Traveling Salesman Problem Brute Force Method

Recursive Linked Lists

Solving Recursive Sequences by Iteration

Functional Dependencies and Normal Forms

The Plurality-with-Elimination Method

Functional Dependencies and Normal Forms

Density Curves Sections

Scope and Parameter Passing

Relational Databases Lecture 2

Scheduling and Digraphs

The Pairwise-Comparison Method

AJAX. Lecture 26. Robb T. Koether. Fri, Mar 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) AJAX Fri, Mar 21, / 16

Introduction to Databases

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

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Sampling Distribution Examples Sections 15.4, 15.5

Introduction to Databases

Magnification and Minification

Basic PHP Lecture 17

Building the Abstract Syntax Trees

DTDs and XML Attributes

Implementing Linked Lists

List Iterator Implementation

XML and AJAX Lecture 28

The Decreasing-Time Algorithm

Example. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review:

XML. Lecture 29. Robb T. Koether. Fri, Mar 28, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XML Fri, Mar 28, / 23

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

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

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Database Connectivity using PHP Some Points to Remember:

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Rotations and Translations

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Stacks and their Applications

The Graphics Pipeline

Javascript Lecture 23

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

MySQL Creating a Database Lecture 3

Friends and Unary Operators

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Binary Tree Applications

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

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

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

The CYK Parsing Algorithm

Recursive Descent Parsers

Programming Languages

Dynamic Allocation of Memory

Recognition of Tokens

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Inheritance: The Fundamental Functions

Displaying Distributions - Quantitative Variables

Inheritance: The Fundamental Functions

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

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Function Definition Syntax Tree

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Street-Routing Problems

The x86 Instruction Set

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

The x86 Architecture

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

The Coefficient of Determination

LR Parsing - The Items

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

XQuery Constructors and Joins Lecture 36

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Transcription:

PHP Querying Lecture 21 Robb T. Koether Hampden-Sydney College Fri, Mar 2, 2018 Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 1 / 32

1 Connect to the Database 2 Querying the Database 3 Searching the Database 4 Deleting from the Database 5 Inserting into a Database 6 Updating the Database 7 Assignment Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 2 / 32

Outline 1 Connect to the Database 2 Querying the Database 3 Searching the Database 4 Deleting from the Database 5 Inserting into a Database 6 Updating the Database 7 Assignment Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 3 / 32

Connect to the Database Connect to the Database $mysqli = new mysqli("localhost", username, password, database); There are a variety of ways to connect to a database. We will use the object-oriented method as opposed to the procedural method. With the single call to the mysqli constructor, we can connect to MySQL and open the database. This function returns a pointer to a mysqli object which, in turn, refers to the database. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 4 / 32

Connect to the Database Test the Connection if ($mysqli->connect_error) die("connection error: ". $mysql->connect_error) There are a variety of ways to connect to a database. We will use the object-oriented method as opposed to the procedural method. With the single call to the mysqli constructor, we can connect to MySQL and open the database. This function returns a pointer to a mysqli object which, in turn, refers to the database. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 5 / 32

Connect to the Database Connect to the Database $mysqli = new mysqli("localhost", "billybob", "euclid", "company"); if ($mysqli->connect_error) die("failed to connect to MySQL: ". $mysqli->connect_error); Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 6 / 32

Outline 1 Connect to the Database 2 Querying the Database 3 Searching the Database 4 Deleting from the Database 5 Inserting into a Database 6 Updating the Database 7 Assignment Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 7 / 32

Querying the Database Querying the Database $query = "SELECT * FROM employees"; $result = $mysqli->query($query) if (!$result) die("query failed: ". $mysqli->error); Suppose that we want to execute the query SELECT * FROM employees; We use the query() function of the mysqli object. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 8 / 32

Querying the Database Querying the Database if ($result->num_rows == 0) echo "No data were not found"; else { // Process the data in the $row array } The query() function, if successful, returns a mysqli_result object that contains an array of rows of data. The data member num_rows is the number of rows in the returned table. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 9 / 32

Querying the Database Querying the Database for ($i = 0; $i < $result->num_rows; $i++) { $row = $result->fetch_array(); // Process the data in the $row array } Use the fetch_array() function to get the first row. On every subsequent call to fetch_array(), it returns the next row. After the last row (or if there were no rows), fetch_array() return null. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 10 / 32

Querying the Database Querying the Database $row = $result->fetch_array(); while ($row!= NULL) { // Process the data in the $row array $row = $result->fetch_array(); } A while loop works, but it is risky. How do you stop an infinite loop when it is running on the remote server? Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 11 / 32

Querying the Database Querying the Database $row = $result->fetch_array(); $fname = $row[ fname ]; $lname = $row[ lname ]; $bdate = $row[ bdate ]; $salary = $row[ salary ]; echo "$fname $lname was born on $bdate and has". " a salary of $salary<br/>"; $row is an associative array. The index is the column name. The value is the entry in the table. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 12 / 32

Outline 1 Connect to the Database 2 Querying the Database 3 Searching the Database 4 Deleting from the Database 5 Inserting into a Database 6 Updating the Database 7 Assignment Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 13 / 32

Searching the Database Search the Database $ssn = "246813579"; $query = "SELECT fname, lname FROM employees WHERE ". "ssn= $ssn "; $result = $mysqli->query($query) or die("query failed: ". $mysqli->error); if ($result->num_rows == 0) echo "Employee with SSN $ssn was not found"; else { $row = $result->fetch_array(); $fname = $row[ fname ]; $lname = $row[ lname ]; echo "$fname $lname was found"; } Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 14 / 32

Searching the Database Now suppose that we want to report the dependents of that employee. We must Search for search the dependents table of the employee s Social Security number. Retrieve the names of his dependents. Report the dependents names. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 15 / 32

Searching the Database Search the Database $query = "SELECT dep_name FROM dependents WHERE ssn= $ssn "; $result = $mysqli->query($query) or die("query failed: ". $mysqli->error); if ($result->num_rows == 0) echo "$fname $lname has no dependents"; else { echo "The dependents of $fname $lname are<br/>"; while ($row = $result->fetch_array()) { $dep_name = $row[ dep_name ]; echo "$dep_name<br/>"; } } Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 16 / 32

Searching the Database Suppose that we want to report the dependents of all employees. We must Execute one query to get the employees names and SSN s. For each SSN in the result, search the dependents table of the employee s to get the names of that employee s dependents. Report the employee s name and the dependents names, or report that the employee has no dependents. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 17 / 32

Outline 1 Connect to the Database 2 Querying the Database 3 Searching the Database 4 Deleting from the Database 5 Inserting into a Database 6 Updating the Database 7 Assignment Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 18 / 32

Deleting from the Database Suppose that we want to delete an employee from the database. We must Select the employee from a list. Delete the employee s tuple from the employees table. Delete the employee s tuples from the dependents and works tables. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 19 / 32

Deleting from the Database In the HTML Form <form method="post" action="del_emp.php"> Select the employee to be deleted: <select name="ssn"> <?php $query = "SELECT fname, lname, ssn FROM employees"; $result = $mysqli->query($query); while ($row = $result->fetch_array()) { $fname = $row[ fname ]; $lname = $row[ lname ]; $ssn = $row[ ssn ]; echo "<option value= $ssn >$fname $lname</option>"; }?> </select><br/> <input type="submit" value="delete Employee"/> <input type="reset"/> </form> Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 20 / 32

Deleting from the Database In the filedel_emp.php $query = "DELETE FROM employees WHERE ssn= $ssn "; $result = $mysqli->query($query) or die("query failed: ". $mysqli->error); $query = "DELETE FROM dependants WHERE ssn= $ssn "; $result = $mysqli->query($query) or die("query failed: ". $mysqli->error); $query = "DELETE FROM works WHERE ssn= $ssn "; $result = $mysqli->query($query) or die("query failed: ". $mysqli->error); Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 21 / 32

Outline 1 Connect to the Database 2 Querying the Database 3 Searching the Database 4 Deleting from the Database 5 Inserting into a Database 6 Updating the Database 7 Assignment Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 22 / 32

Inserting into a Database We will use the company database for our examples. Suppose that we want to add an employee to the database. We must get from the user the employee s First and last names. Social Security number. Sex. Birthday. Salary. Department. Then insert these data into the employees table. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 23 / 32

The HTML Form In the HTML form, we need Text boxes for the first name, last name, Social Security number, and salary. Select elements for the sex, birthday, and department. Select elements for the birthday (month, day, year). Select elements or radio buttons for the sex and department. We can fill the sex and birthday choices with the obvious values. For the department, we should construct the list of options from the database, using the departments table. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 24 / 32

Inserting into a Database Getting the Birthday <?php echo "<select name= month >"; $month = array("january", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); for ($i = 0; $i < count($month); $i++) echo "<option value= $i+1 >$month[$i]</option>"; echo "</select>"; echo "<select name= day >"; for ($i = 1; $i <= 31; $i++) echo "<option value= $i >$i</option>"; echo "</select>"; echo "<select name= year >"; for ($i = 2014; $i >= 1914; $i--) echo "<option value= $i >$i</option>"; echo "</select><br/>";?> Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 25 / 32

Inserting into a Database Getting the Department <?php echo "<select name= dept >"; $query = "SELECT dept, dept_name FROM departments"; $result = $mysqli->query($query) or die("query failed: ". $mysqli->error); while ($row = $result->fetch_array()) { $dept = $row[ dept ]; $dept_name = $row[ dept_name ]; echo "<option value= $dept >$dept_name</option>"; } echo "</select><br/>";?> Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 26 / 32

Inserting into the Database Insert into the Database $fname = $_POST[ fname ]; $lname = $_POST[ lname ]; $ssn = $_POST[ ssn ]; $salary = $_POST[ salary ]; $sex = $_POST[ sex ]; $month = $_POST[ month ]; $day = $_POST[ day ]; $year = $_POST[ year ]; $dept = $_POST[ dept ]; $query = "SELECT * FROM employees WHERE ssn = $ssn "; $result = $mysqli->query($query); if ($result->num_rows > 0) echo "$fname $lname is already in the database"; We must get the form information and find out whether the employee is already in the database. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 27 / 32

Inserting into the Database Insert into the Database else { if ($month < 10) $month = "0$month"; if ($day < 10) $day = "0$day"; $bdate = "$year-$month-$day"; $query = "INSERT INTO employees VALUES( $fname, $lname, ". " $ssn, $bdate, $sex, $salary, $dept)"; $mysqli->query($query) or die("query failed: ". $mysqli->error); echo "$fname $lname has been inserted." } If he is not already in the database, then add him. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 28 / 32

Outline 1 Connect to the Database 2 Querying the Database 3 Searching the Database 4 Deleting from the Database 5 Inserting into a Database 6 Updating the Database 7 Assignment Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 29 / 32

Updating the Database We want to update the data for an employee. We must Select the employees name and Social Security number from the employees table. Display the current employee information. Allow the information to be edited. Save the changes. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 30 / 32

Outline 1 Connect to the Database 2 Querying the Database 3 Searching the Database 4 Deleting from the Database 5 Inserting into a Database 6 Updating the Database 7 Assignment Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 31 / 32

Assignment Assignment Visit the W3Schools website http://www.w3schools.com/html/ Visit the sections labeled PHP MySQL Intro through PHP DELETE. Visit the PHP website http://php.net/manual/en/book.mysql.php and check out the functions that we used. Robb T. Koether (Hampden-Sydney College) PHP Querying Fri, Mar 2, 2018 32 / 32