Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.

Slides:



Advertisements
Similar presentations
PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Advertisements

PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement.
Programming MySQL, Perl COEN 351. Reading List Paul DuBois: MySQL and Perl for the Web, New Riders, 2002 Jacqueline D. Hamilton: CGI Programming 101,
Keys, Referential Integrity and PHP One to Many on the Web.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2014 All Rights Reserved.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Manipulating MySQL Databases with PHP. PHP and mySQL2 Objectives Connect to MySQL from PHP Learn how to handle MySQL errors Execute SQL statements with.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Objectives Connect to MySQL from PHP
Introduction to PHP and MySQL Kirkwood Center for Continuing Education By Fred McClurg, © Copyright 2014, Fred McClurg, All Rights.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
PHP Programming. Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling.
Deleting and Updating Records in MySQL using PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Class 3 MySQL Robert Mudge Reference:
1 Chapter 8 – Working with Databases spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
INTERNET APPLICATION DEVELOPMENT For More visit:
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 19: Database Support.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
Lec_6 Manipulating MySQL Databases with PHP PHP Programming with MySQL.
MySQL Databases & PHP Integration Using PHP to write data to, and retrieve data from, a MySQL database.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
PHP Part 2.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
PHP with MySQL 1.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
CHAPTER 9 PHP AND MYSQL. A POSSIBLE SITE CONFIGURATION Application Folder index.php includes (folder)header.phpfooter.phpstyle.cssmodel (folder)mysqli_connect.php.
2010/11 : [1]PHP with MySQLBuilding Web Applications using MySQL and PHP (W1) PHP with MySQL.
PHP Database connectivity Connecting with RDBMS and editing, adding, and deleting databases therein are all done through PHP functions.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
PHP getting data from a MySQL database. Replacing XML as data source with MySQL Previously we obtained the data about the training session from an XML.
>> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the.
Creating A Database Driven Website 1.Setting Up Your Web Server 2.Creating a Database 3.Creating a Webpage to Display Information From a Database 4.Creating.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
MySQL. Is a SQL (Structured Query Language) database server. Can be accessed using PHP with embedded SQL Queries Supports Large DB’s, 60,000 tables with.
PHP Programming. Topics Database Handling (MySQL, MSSQL, ODBC)
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
>> PHP: MySQL & CRUD. R ecall Database Tables Records is composed of Operations (CRUD) Create Retrieve Update Delete DBMS Access Control MySQL phpMyAdmin.
CSC 2720 Building Web Applications Accessing MySQL from PHP.
MySQL MySQL and PHP – interacting with a database.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015 All Rights Reserved.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2016, Fred McClurg, All Rights.
PHP and SQL Server: Connection IST 210: Organization of Data IST2101.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
IS1500: Introduction to Web Development
Web Design and Development
Introduction to Web programming
Objectives Connect to MySQL from PHP Learn how to handle MySQL errors
Kirkwood Center for Continuing Education
Introduction to Web programming
Presentation transcript:

Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights Reserved.

Chapter Ten Getting PHP to talk to MySQL es/chapter10.phpAndMysql.ppt 2

Regardless of whether you use the MySQL command- line, the built-in functions, or the PEAR functions, you will use the same process to communicate to the database. Common Process Steps: 0. Initialize variables 1. Connect to the database. 2. Choose which database to use. 3. Build the SQL statement. 4. Execute the SQL statement. 5. Retrieve and display the results. 6. Close database connection (optional) Performing the Perfect Procedure 3

File “login.inc.php” page 1: <?php // 0. init DB variables $host = "localhost"; $username = "root"; $password = ""; $dbName = "cookbookdb";... ?> Step 0: Initialize Variables 4

File “login.inc.php” page 2: <?php... // Steps: 1 & 2. Connect and use database $conn = mysqli_connect( $host, $username, $password, $dbName ); // check connection (if not connected) if ( mysqli_connect_errno() ) { die( "Could not connect $host to database. ". mysqli_connect_error() ); }... ?> Steps 1 & 2: Connection to Database 5

Description: The Perl motto of “There Is More Than One Way To Do It” is also true of Perl’s cousin PHP. The acroynm “TIMTOWTDI” (pronounced “Tim Toady”), is especially true concerning PHP and MySQL. Built-in PHP connect function: mysqli_connect( $host, $username, $password, $dbName ); PEAR DB connect function: $conn = DB::connect("mysql:://$username: Making a PHP Connection to MySQL 6

File “fetch_row.php” page 1: <?php // 0, 1, 2: Connect & use db // 0, 1, 2: Connect & use db require_once( "login.php" ); require_once( "login.php" ); // 3. Build the SQL statement // 3. Build the SQL statement $sql = "SELECT * $sql = "SELECT * FROM recipe"; FROM recipe";......?> Step 3: Define SQL Statement 7

File “fetch_row.php” page 2: <?php... // Step 4: Execute the SQL statement $result = mysqli_query($conn, $sql); if ( ! $result ) // sql failed { die( "Could not execute SQL '$sql' ". mysqli_error() ); }... ?> Step 4: Execute SQL Statement 8

Description: The function “mysqli_fetch_row()” retrieves the results from a database query via a numeric array. See file “fetch_row.php” page 3 <?php... // Step: 5. Display results as numeric array while ( $row = mysqli_fetch_row( $result ) ) { printf( "ID: %s Name: %s ", $row[0], $row[1] ); } // $conn initialized in "login.inc.php" mysqli_close( $conn ); ?> Step 5: Retrieve Database Results 9

Description: The function “mysqli_fetch_assoc()” retrieves the results from a database query via a hash. See file “fetch_assoc.php” <?php... // Step: 5. Display results as associative array while ( $hash = mysqli_fetch_assoc( $result ) ) { printf( "ID: %s Name: %s ", $hash['id'], $hash['name'] ); } // $conn initialized in "login.inc.php" mysqli_close( $conn ); ?> Step 5: Retrieve DB Results (alternate) 10

Student Exercise 9.1 Summary TitleMixType Chocolate Chip Cookies Add chocolate chips... Dessert... Description: Write a PHP program that displays the following information from the database in the browser: 11

Requirements: 1.Using MySQL command-line or phpmyadmin, create a database named “cookbook”. 2.Inside the cookbookdb database, import the file “cookbookDB.sql” to create the tables and data. chapter10/cookbookdb.sql chapter10/cookbookdb.sql 3.Inside the file “list.php”, perform a database query joining the tables “recipe” and “category”. Display the columns “recipe.name”, “recipe.content”, “category.name” and create aliases for them. Sort the results by “recipe.name”. 4.Retrieve the database rows using “mysqli_fetch_assoc()”. 5.Display the results in an HTML table. Student Exercise 9.1 Details 12

See file “list.1.php”. Build the SQL statement. <?php... // 3. Build the SQL statement $sql = " SELECT rec.name AS title, rec.content AS mix, cat.name AS type FROM recipe AS rec, category AS cat WHERE rec.category_id = cat.id ORDER BY title";... ?> Student Exercise 9.1 Solution (Pg 1) 13

Description: The table header. (see file “list.1.php”) <?php... printf(" "); printf(" Title "); printf(" Mix "); printf(" Type "); printf(" ");... ?> Student Exercise 9.1 Solution (Pg 2) 14

Description: The body of the table. (see file “list.1.php”). Anybody remember “TIMTOWDI”? <?php... // 5. Display the results while ( $row = mysqli_fetch_assoc( $result ) ) { /* multiple print statements */ printf(" \n" ); printf(" %s \n", $row['title'] ); printf(" %s \n", $row['mix'] ); printf(" %s \n", $row['type'] ); printf(" \n"); } printf( " \n" );... ?> Student Exercise 9.1 Solution (Pg 3, option 1) 15

Description:... and the body of the while loop. (see file “list.2.php”) <?php... // 5. Display the results while ( $row = mysqli_fetch_assoc( $result ) ) { /* single print statement */ printf( " %s \n", $row['title'], $row['mix'], $row['type'] ); } printf( " \n" );... ?> Student Exercise 9.1 Solution (Pg 3, option 2) 16

Description:... and the body of the while loop. (see file “list.3.php”) <?php... // 5. Display the results while ( $row = mysqli_fetch_assoc( $result ) ) { /* escape PHP and begin HTML */ ?> <?php /* escape HTML and begin PHP */ } printf( " \n" );... ?> Student Exercise 9.1 Solution (Pg 3, option 3) 17

Description:... and the body of the while loop. (see file “list.4.php”) <?php... // HTML table creation functions require_once( "table.inc.php" );... // create table head via function $header = array( "Title", "Mix", "Type" ); PrintTableHeader( $header ); // 5. Display the results while ( $row = mysqli_fetch_assoc( $result ) ) { // create table rows via function PrintTableRow( $row ); } printf( " \n" );... ?> Student Exercise 9.1 Solution (Pg 3, option 4) 18

Description: Included HTML table functions. (see file “table.inc.php”) <?php function PrintTableHeader( $columns ) { $bgColor = "#b4cdcd"; // LightCyan3 $fgColor = "#000000"; // Black printf( "<table border='1' cellpadding='5' cellspacing='0'>" ); printf( " ", $bgColor, $fgColor ); foreach ( $columns as $name ) printf( " %s ", $name ); printf( " " ); } // function PrintTableHeader(); ?> Student Exercise 9.1 Solution (Pg 4, option 4) 19

Description: Included HTML table functions. (see file “table.inc.php”) <?php function PrintTableRow( $row ) { static $rowCount = 0; $fgColor = "#000000"; // Black if ( $rowCount % 2 ) // not even $bgColor = "#d1eeee"; // LtCyan2 else $bgColor = "#e0ffff"; // LtCyan printf( " \n", $bgColor, $fgColor ); foreach ( $row as $key => $value ) printf( " %s \n", $row[$key] ); printf( " \n" ); $rowCount++; } // function PrintTableRow(); ?> Student Exercise 9.1 Solution (Pg 5, option 4) 20

to be continued... rg/courses/php/slides/chapter11.ppt 21