Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.

Similar presentations


Presentation on theme: "Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash."— Presentation transcript:

1

2 Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash

3 Copyright © 2003 Pearson Education, Inc. Slide 8-2 CHAPTER 8 Using Databases with PHP Scripts: Using MySQL Database with PHP

4 Copyright © 2003 Pearson Education, Inc. Objectives To understand the advantages of using databases to store Web data To learn how to prepare a MySQL database for use with PHP To learn how to store, retrieve, and update data in a MySQL database

5 Copyright © 2003 Pearson Education, Inc. Slide 8-4 What is a database? A set of data organized into one or more computer files. Using files for product inventory is a type of database Generally the term is reserved for more formal database systems like access, Oracle or MySQL.

6 Copyright © 2003 Pearson Education, Inc. Slide 8-5 Advantages of Databases Over Files Faster access Better concurrent access Easier changes to data and scripts Increased security

7 Copyright © 2003 Pearson Education, Inc. Slide 8-6 Relational Database? Relational databases store data in tables (usually more than one) with defined relationships between the tables.

8 Copyright © 2003 Pearson Education, Inc. Slide 8-7 Which Database System PHP works with a variety of databases that include: Oracle Access Ingres SQL Server MySQL We will be using MySQL since its simple to use, free and very popular.

9 Copyright © 2003 Pearson Education, Inc. Slide 8-8 Using A Query Language When using a database, we use a separate query language called the Structured Query Language (SQL).

10 Copyright © 2003 Pearson Education, Inc. Slide 8-9 Creating a Database Instance Once you have access to a server with MySQL installed, you need a database instance created for you (which I have done). Usually created by a database administrator Creates a database instance, userid and password.

11 Copyright © 2003 Pearson Education, Inc. Slide 8-10 Creating Your Database Tables Once the database instance is created you need to create your tables. Use SQL CREATE TABLE command

12 Copyright © 2003 Pearson Education, Inc. Slide 8-11 Other Data Types? MySQL supports many other data types beyond TEXT and INT. Here are a few : TEXT specifies that the table column can hold a large amount of character data. It can use space inefficiently since it reserves space for up to 65,535 characters. CHAR(N) specifies a table column that holds a fixed length string of up to N characters (N must be less than 256). VARCAR(N) specifies a table column that holds a variable length string of up to N characters and removes any unused spaces on the end of the entry.

13 Copyright © 2003 Pearson Education, Inc. Slide 8-12 Other Data Types? INT specifies a table column that holds an integer with a value from about –2 billion to about 2 billion. INT UNSIGNED specifies a table column that holds an integer with a value from 0 to about 4 billion. SMALLINT specifies a table column that holds an integer with a value from –32,768 to 32,767. SMALLINT UNSIGNED specifies a table column that holds an integer with a value from 0 to 65,535. DECIMAL(N,D) specifies a number that supports N total digits, of which D digits are to the right of the decimal point.

14 Copyright © 2003 Pearson Education, Inc. Slide 8-13 Some additional CREATE TABLE Options Can specify some additional options in CREATE TABLE:

15 Copyright © 2003 Pearson Education, Inc. Slide 8-14 Issuing CREATE TABLE From PHP Script Segment 1. $connect = mysqli_connect($server, $user, $pass); 2. if ( !$connect ) { 3. die ("Cannot connect to $server using $user"); 4. } else { 5. mysqli_select_db('MyDatabaseName'); 6. $SQLcmd = 'CREATE TABLE Products( ProductID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, Product_desc VARCHAR(50), Cost INT, Weight INT, Numb INT )'; 7. mysqli_query($SQLcmd, $connect); 8. mysqli_close($connect); 9. } Issue the SQL query to the database. Connect to MySQL

16 Copyright © 2003 Pearson Education, Inc. Slide 8-15 Full Script Show Source code.

17 Copyright © 2003 Pearson Education, Inc. Slide 8-16 Script Browser Output Execute code: http://itm325.itmbsu.net/jstudent/Examples/Ch8/table_create.phphttp://itm325.itmbsu.net/jstudent/Examples/Ch8/table_create.php

18 Copyright © 2003 Pearson Education, Inc. Slide 8-17 Inserting Data Once the database and table are created you will need to insert data Use the SQL INSERT command

19 Copyright © 2003 Pearson Education, Inc. Slide 8-18 A Full Example Consider an application that allows end-user to enter inventory data: Item Description: <input type="text" size="20" maxlength="20" name="Item"> Weight: <input type="text" size="5" maxlength="20" name="Weight"> Cost: <input type="text" size="5" maxlength="20" name="Cost"> Number Available:<input type="text" size="5" maxlength="20" name="Quantity">

20 Copyright © 2003 Pearson Education, Inc. Slide 8-19 Receiving PHP Script Show source code.

21 Copyright © 2003 Pearson Education, Inc. Slide 8-20 Script Output This script can be executed at: http://itm325.itmbsu.net/jstudent/Examples/Ch8/insert.html http://itm325.itmbsu.net/jstudent/Examples/Ch8/insert.html

22 Copyright © 2003 Pearson Education, Inc. Slide 8-21 Retrieving Data Two major ways to retrieve data: 1. Retrieving all elements from a table 2. Searching for specific records in a table

23 Copyright © 2003 Pearson Education, Inc. Slide 8-22 Retrieving Data To retrieve all data, use the following SQL command For example 1. $connect = mysqli_connect('Localhost', 'phppgm', 'mypasswd'); 2. $SQLcmd = 'SELECT * FROM Products'; 3. mysqli_select_db('MyDatabase'); 4. $results_id = mysqli_query($SQLcmd, $connect);

24 Copyright © 2003 Pearson Education, Inc. Slide 8-23 Using mysqli_fetch_row() Use the mysqli_fetch_row() function to retrieve data one row at a time while ( $row = mysqli_fetch_row($results_id)) { foreach ( $row as $field ) { print "Field=$field”; } } Access each field in the table row results. Access each row from the mysqli_query() results. (A different row each iteration). $results_id variable is set from mysqli_query() function call. Output each item of the $row array.

25 Copyright © 2003 Pearson Education, Inc. Slide 8-24 A Script Example Show Source Code

26 Copyright © 2003 Pearson Education, Inc. Slide 8-25 Script Output This script can be executed at: http://itm325.itmbsu.net/jstudent/Examples/Ch8/query.html http://itm325.itmbsu.net/jstudent/Examples/Ch8/query.html

27 Copyright © 2003 Pearson Education, Inc. Slide 8-26 Searching For Specific Records Use the SELECT SQL statement with a WHERE clause SELECT * FROM TableName WHERE (test_expression); The asterisk (“*”) means look at all table columns. Specify the table name to look at. Specify a test expression to evaluate

28 Copyright © 2003 Pearson Education, Inc. Slide 8-27 Selected WHERE CLAUSE Test Operators

29 Copyright © 2003 Pearson Education, Inc. Slide 8-28 Consider the following example … The following example searches a hardware inventory database for a specific part name entered by the user. The form uses the following key HTML form element definition.

30 Copyright © 2003 Pearson Education, Inc. Slide 8-29 PHP Source Show source code

31 Copyright © 2003 Pearson Education, Inc. Slide 8-30 Would have the following output … This script can be executed at: http://itm325.itmbsu.net/jstudent/Examples/Ch8/search.html http://itm325.itmbsu.net/jstudent/Examples/Ch8/search.html

32 Copyright © 2003 Pearson Education, Inc. Slide 8-31 Updating a Database Record Use SQL UPDATE command when needing to update a database record: UPDATE Table_name SET col1=chng_express1,col2=chng_express2,... WHERE test_expression Specify the name of the table to update. Specify one or more table column to receive the results of an expression. Optionally specify a WHERE Optionally specify a WHERE clause and test expression.

33 Copyright © 2003 Pearson Education, Inc. Slide 8-32 For Example … The following looks through the Products table for values of Product_desc equal to Hammer. When it finds it, it decrements the Count column value by 1. UPDATE Products SET Count=Count-1 WHERE 'Product_desc=Hammer'

34 Copyright © 2003 Pearson Education, Inc. Slide 8-33 A Full Example … Consider the following example Displays current inventory Asks end-user to decrement value for 1 item Uses the following HTML Hammer Screwdriver Wrench<input type="radio" name="Product“ value="Wrench"

35 Copyright © 2003 Pearson Education, Inc. Slide 8-34 Full Example Show Source Code

36 Copyright © 2003 Pearson Education, Inc. Slide 8-35 Would output the following: Execute this example at http:http://itm325.itmbsu.net/jstudent/Examples/Ch8/sale.html http:http://itm325.itmbsu.net/jstudent/Examples/Ch8/sale.html

37 Copyright © 2003 Pearson Education, Inc. Slide 8-36 Use of the mysqli_fetch_array() function The mysqli_fetch_array(), by default, returns an array that can be read either as a sequential array or as an associative array. You can read more about it here.here Here is an example using the mysql_fetch_array()example Here is the source code.source code

38 Copyright © 2003 Pearson Education, Inc. Summary A database is a set of data organized into one or more computer files. Relational databases store data in tables Before you can start to work with a MySQL database, you must install a copy of MySQL on your personal computer or Web server, create a database instance for your script and create your initial tables by issuing the SQL CREATE TABLE command. Use the SQL SELECT statement to retrieve data from a MySQL database and include a WHERE clause to select specific table rows..

39 Copyright © 2003 Pearson Education, Inc. Slide 8-38 Summary - II Use an SQL UPDATE statement to change records in a MySQL database. Include a WHERE clause to select specific table rows and a SET clause to define change expressions.


Download ppt "Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash."

Similar presentations


Ads by Google