Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights."— Presentation transcript:

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

2 Chapter Ten Getting PHP to talk to MySQL http://cecert.kirkwood.edu/~fmcclurg/courses/php/slid es/chapter10.phpAndMysql.ppt 2

3 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

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

5 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

6 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: $password@$host/$database"); Making a PHP Connection to MySQL 6

7 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

8 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

9 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

10 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

11 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

12 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. http://cecert.kirkwood.edu/~fmcclurg/courses/php/examples/ chapter10/cookbookdb.sql http://cecert.kirkwood.edu/~fmcclurg/courses/php/examples/ 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 to be continued... http://cecert.kirkwood.edu/~fmcclu rg/courses/php/slides/chapter11.ppt 21


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

Similar presentations


Ads by Google