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

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Advertisements

PHP and MySQL Database. Connecting to MySQL Note: you need to make sure that you have MySQL software properly installed on your computer before you attempt.
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Objectives Connect to MySQL from PHP
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Concepts of Database Management Sixth Edition
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
ADVM420- Class #4 Web Design with PHP and MySQL Adding and Listing from a MySQL Database.
1 CS428 Web Engineering Lecture 23 MySQL Basics (PHP - VI)
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
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.
Slide 8-1 CHAPTER 8 Using Databases with PHP Scripts: Using MySQL Database with PHP.
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:
ASP.NET Programming with C# and SQL Server First Edition
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Nic Shulver, Retrieving Stored Data Introduction This set of slides shows: The information source database structure The data.
Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
PHP – MySQL Extensions. Table used in most examples CREATE TABLE product ( rowID INT NOT NULL AUTO_INCREMENT, productid VARCHAR(8) NOT NULL, name VARCHAR(25)
Concepts of Database Management Seventh Edition
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
1 Working with MS SQL Server Textbook Chapter 14.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Working with MSSQL Server Code:G0-C# Version: 1.0 Author: Pham Trung Hai CTD.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null.
CSC 2720 Building Web Applications Database and SQL.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
Database and mySQL Week 07 Dynamic Web TCNJ Jean Chu.
Database Fred Durao What is a database? A database is any organized collection of data. Some examples of databases you may encounter in.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Enterprise PHP – Reading Data from a DB Reading Data from a relational database in PHP Nic Shulver, FCES, Staffordshire University Using the SQLi interface.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
CHAPTER 10 PHP MySQL Database
Relational Databases and MySQL. Relational Databases Relational databases model data by storing rows and columns in tables. The power of the relational.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
The Web Wizard’s Guide to PHP by David Lash
Web Database Programming Using PHP
3 A Guide to MySQL.
Chapter 5 Introduction to SQL.
Web Database Programming Using PHP
ORACLE SQL Developer & SQLPLUS Statements
ISC440: Web Programming 2 Server-side Scripting PHP 3
Chapter 8 Working with Databases and MySQL
Presentation transcript:

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

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

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

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.

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

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.

Copyright © 2003 Pearson Education, Inc. Slide 8-7 Which Database System PHP works with a variety of databases that include: Oracle Access Ingres Microsoft SQL Server MySQL In this chapter, we’ll look at PHPs interface with MySQL since simple to use, free and very popular.

Copyright © 2003 Pearson Education, Inc. Slide 8-8 Using A Query Language When using a database, use a separate query language to work with database Within MySQL and other SQL-based servers, use Structured Query Language (SQL), to access database

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

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

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.

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.

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

Copyright © 2003 Pearson Education, Inc. Slide 8-14 Issuing CREATE TABLE From PHP Script Segment 1. $connect = mysql_connect($server, $user, $pass) 2. or die ("Cannot connect to $server using $user"); 3. mysql_select_db('MyDatabaseName'); 4. $SQLcmd = 'CREATE TABLE Products( ProductID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, Product_desc VARCHAR(50), Cost INT, Weight INT, Numb INT )'; 5. mysql_query($SQLcmd, $connect); 6. mysql_close($connect); Issue the SQL query to the database. Connect to MySQL

Copyright © 2003 Pearson Education, Inc. Slide 8-15 Full Script 1. Create Table 2. <?php 3. $server = 'localhost'; 4. $user = 'phppgm'; 5. $pass = 'mypasswd'; 6. $mydb = 'mydatabase'; 7. $table_name = 'Products'; 8. $connect = mysql_connect($server, $user, $pass) 9. Or die ("Cannot connect to $server using $user"); 10.$SQLcmd = "CREATE TABLE $table_name ( ProductID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, Product_desc VARCHAR(50), Cost INT, Weight INT, Numb INT)";

Copyright © 2003 Pearson Education, Inc. Slide 8-16 Full Script continued … 13. mysql_select_db($mydb); 14. if (mysql_query($SQLcmd, $connect)){ 15. print ' Created Table'; 16. print " $table_name in database $mydb "; 17. print " SQLcmd=$SQLcmd"; 18. } else { 19. die ("Table Create Creation Failed SQLcmd=$SQLcmd"); 20. } 21. mysql_close($connect); 22. } 23. ?>

Copyright © 2003 Pearson Education, Inc. Slide 8-17 Script Browser Output

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

Copyright © 2003 Pearson Education, Inc. Slide 8-19 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">

Copyright © 2003 Pearson Education, Inc. Slide 8-20 Receiving PHP Script 1. Insert Results 2. <?php 3. $host = 'localhost'; 4. $user = 'phppgm'; 5. $passwd = 'mypasswd'; 6. $database = 'mydatabase'; 7. $connect = mysql_connect($host, $user, $passwd); 8. $table_name = 'Products'; 9. $query = "INSERT INTO $table_name VALUES ('0','$Item','$Cost','$Weight','$Quantity')"; 10. print "The Query is $query "; 11. mysql_select_db($database); 12. print ' '; 13. if (mysql_query($query, $connect)){ 14. print "Insert into $database was successful! "; 15. } else { 16. print "Insert into $database failed! "; 17. } mysql_close ($connect); 18. ?>

Copyright © 2003 Pearson Education, Inc. Slide 8-21 Script Output

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

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

Copyright © 2003 Pearson Education, Inc. Slide 8-24 Using mysql_fetch_row() Use the mysql_fetch_row() function to retrieve data on row at a time Use the mysql_fetch_array() function to retrieve data on one row at a time as an associative array with the indices being the column names from the data retrieved

Copyright © 2003 Pearson Education, Inc. Slide 8-25 A Script Example 1. Table Output 2. <?php 3. $host= 'localhost'; 4. $user = 'phppgm'; 5. $passwd = 'mypasswd'; 6. $database = 'phppgm'; 7. $connect = mysql_connect($host, $user, $passwd); 8. $table_name = 'Products'; 9. print ' '; 10. print "$table_name Data "; 11. $query = "SELECT * FROM $table_name"; 12. print "The query is $query "; 13. mysql_select_db($database); 14. $results_id = mysql_query($query, $connect); 15. if ($results_id) { 16.print ' '; 17. print ' Num Product Cost Weight Count';

Copyright © 2003 Pearson Education, Inc. Slide 8-26 A Script Example 18. while ($row = mysql_fetch_row($results_id)){ 19. print ' '; 20. foreach ($row as $field) { 21. print " $field "; 22.} 23. print ' '; 24. } 25. } else { die ("Query=$query failed!"); } 26. mysql_close($connect); 27. ?>

Copyright © 2003 Pearson Education, Inc. Slide 8-27 Script Output

Copyright © 2003 Pearson Education, Inc. Slide 8-28 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

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

Copyright © 2003 Pearson Education, Inc. Slide 8-30 Selected WHERE CLAUSE Wildcard Operators You can use wildcards with the LIKE comparison operator in your WHERE clauses for “fuzzy” searches Use the _ (underline) symbol to replace a single character WHERE (AC like ‘6_8’) would match 618, 628, etc. Use the % (percent) symbol to match multiple characters WHERE (AC like ‘6%’) would match 6, 66, 61,

Copyright © 2003 Pearson Education, Inc. Slide 8-31 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.

Copyright © 2003 Pearson Education, Inc. Slide 8-32 PHP Source 1. Search Results 2. <?php 3. $host= 'localhost'; 4. $user = 'phppgm'; 5. $passwd = 'mypasswd'; 6. $database = 'phppgm'; 7. $connect = mysql_connect($host, $user, $passwd); 8. $table_name = 'Products'; 9. print ' '; 10.print "$table_name Data "; 11.$query = "SELECT * FROM $table_name WHERE (Product_desc = '$Search')"; 12.print "The query is $query "; 13.mysql_select_db($database); 14.$results_id = mysql_query($query, $connect);

Copyright © 2003 Pearson Education, Inc. Slide 8-33 PHP Source II 15. if ($results_id) { 16.print ' '; 17. print ' Num Product Cost Weight Count'; 18. while ($row = mysql_fetch_row($results_id)) { 19. print ' '; 20. foreach ($row as $field) { 21. print " $field "; 22. } 23. print ' '; 24. } 25. } else { die ("query=$Query Failed");} 26. mysql_close($connect); 27. ?>

Copyright © 2003 Pearson Education, Inc. Slide 8-34 Would have the following output …

Copyright © 2003 Pearson Education, Inc. Slide 8-35 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.

Copyright © 2003 Pearson Education, Inc. Slide 8-36 WARNING!!! Do not run an update query on a table in an SQL database without a WHERE clause unless you really want to change the information for ALL records in the database. You should normally use the Primary Key in the condition in the WHERE clause to select the record that you want to change

Copyright © 2003 Pearson Education, Inc. Slide 8-37 For Example … The following searches the Products table for values of Product_desc equal to Hammer and sets the Cost column to a value of 2. UPDATE Products SET Cost=2 WHERE (Product_desc=‘Hammer’)

Copyright © 2003 Pearson Education, Inc. Slide 8-38 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’)

Copyright © 2003 Pearson Education, Inc. Slide 8-39 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"

Copyright © 2003 Pearson Education, Inc. Slide 8-40 Full Example I 1. Product Update Results 2. <?php 3. $host= 'localhost'; 4. $user = 'phppgm'; 5. $passwd = 'mypasswd'; 6. $database = 'phppgm'; 7. $connect = mysql_connect($host, $user, $passwd); 8. $table_name = 'Products'; 9. print ' '; 10.print "Update Results for Table $table_name \n"; 11.$query = "UPDATE $table_name SET Numb = Numb-1 WHERE (Product_desc = '$Product')"; 12.print "The query is $query \n"; 13.mysql_select_db($database);

Copyright © 2003 Pearson Education, Inc. Slide 8-41 A Full Example II … 14. $results_id = mysql_query($query, $connect); 15. if ($results_id){ 16. Show_all($connect, $database,$table_name); 17. } else { 18. print "Update=$query failed"; 19. } 20. mysql_close($connect);

Copyright © 2003 Pearson Education, Inc. Slide 8-42 A Full Example III … 21. function Show_all($connect, $database, $table_name){ 22. $query = "SELECT * from $table_name"; 23. $results_id = mysql_query($query, $connect); 24. print ' Num Product Cost Weight Count'; 26. while ($row = mysql_fetch_row($results_id)) { 27. print ' '; 28. foreach ($row as $field){ 29. print " $field "; 30. } 31. print ' '; 32. } 33. } 34. ?>

Copyright © 2003 Pearson Education, Inc. Slide 8-43 Would output the following:

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..

Copyright © 2003 Pearson Education, Inc. Slide 8-45 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.