Chapter 4 How to use PHP with a MySQL database Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc. Slide 1

Download Report

Transcript Chapter 4 How to use PHP with a MySQL database Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc. Slide 1

Chapter 4 How to use PHP with a MySQL database

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 1

A method of the PDOStatement class for getting an array for a row

fetch()

Code that gets a result set that contains one row

$query = 'SELECT productCode, productName, listPrice FROM products WHERE productID = $productID'; $products = $db->query($query); // $products is a PDOStatement object $product = $products->fetch();

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 2

Code that uses a string index to get each column

$product_code = $product['productCode']; $product_name = $product['productName']; $product_list_price = $product['listPrice'];

Code that uses a numeric index to get each column

$product_code = $product[0]; $product_name = $product[1]; $product_list_price = $product[2];

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 3

A query method that returns a result set of two or more rows

$query = 'SELECT productCode, productName, listPrice FROM products WHERE categoryID = 1;' $products = $db->query($query); // $products contains the result set

How to use a foreach statement to display the result set in an HTML table

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 4

Another syntax for the foreach statement that works better within PHP tags

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 5

A more user-friendly interface

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 6

The user interface after the user selects a new category

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 7

The Product View Application Code Separated into three files database.php database_error.php index.php

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 8

The database.php file

getMessage(); include('database_error.php'); exit(); } ?>

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 9

The database_error.php file

My Guitar Shop

Database Error

There was a database connection error.

The database must be installed.

MySQL must be running.

Error message:

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 10

The index.php file query($query); $category = $category->fetch(); $category_name = $category['categoryName']; // Get all categories $query = 'SELECT * FROM categories ORDER BY categoryID'; $categories = $db->query($query); // Get products for selected category $query = "SELECT * FROM products WHERE categoryID = $category_id ORDER BY productID"; $products = $db->query($query); ?>

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 11

The index.php file (continued)

My Guitar Shop

Product List

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 12

The index.php file (continued)

Code Name Price

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 13

The index.php file (continued)

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 14

The Product List page

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 15

The Add Product page

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 16

The index.php file

query($query); $category = $category->fetch(); $category_name = $category['categoryName'];

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 17

The index.php file (continued)

// Get all categories $query = 'SELECT * FROM categories ORDER BY categoryID'; $categories = $db->query($query); // Get products for selected category $query = "SELECT * FROM products WHERE categoryID = $category_id ORDER BY productID"; $products = $db->query($query); ?>

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 18

The index.php file (continued)

My Guitar Shop

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 19

The index.php file (continued)

Product List

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 20

The index.php file (continued)

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 21

The index.php file (continued)

Code Name Price  

Add Product

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 22

The index.php file (continued)

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 23

The delete_product.php file

exec($query); // Display the Product List page include('index.php'); ?>

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 24

The add_product_form.php file

query($query); ?> My Guitar Shop

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 25

The add_product_form.php file (continued)

Add Product


Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 26

The add_product_form.php file (continued)



View Product List

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 27

The add_product.php file

exec($query);

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 28

The add_product.php file (continued)

// Display the Product List page include('index.php'); } ?>

Murach's PHP and MySQL, C4 © 2010, Mike Murach & Associates, Inc.

Slide 29