PHP and MySQL. Why Use a Database  Easy access to data  Simultaneous access by multiple users is handled properly  Security - easy to control access.

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

Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement.
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.
Introduction The concept of “SQL Injection”
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
PHP (2) – Functions, Arrays, Databases, and sessions.
Manipulating MySQL Databases with PHP. PHP and mySQL2 Objectives Connect to MySQL from PHP Learn how to handle MySQL errors Execute SQL statements with.
Objectives Connect to MySQL from PHP
MySQL and PHP By Trevor Adams.
Databases Using MySQL Creating Tables Queries. Databases  A database is a collection of data organized for efficient access  A relational database is.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
Databases with PHP A quick introduction. Y’all know SQL and Databases  You put data in  You get data out  You can do processing on it very easily 
Session 5: Working with MySQL iNET Academy Open Source Web Development.
LIS651 lecture 7 PHP mySQL Thomas Krichel
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 &
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Analysis of SQL injection prevention using a proxy server By: David Rowe Supervisor: Barry Irwin.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Database Access Through the Web.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 19: Database Support.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
PHP Part 2.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
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.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
Session 7: Getting PHP to Talk to MySQL. Objectives Connecting to the Database Building & Executing the SQL SELECT Query Fetching & Displaying the data.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
CHAPTER 9 PHP AND MYSQL. A POSSIBLE SITE CONFIGURATION Application Folder index.php includes (folder)header.phpfooter.phpstyle.cssmodel (folder)mysqli_connect.php.
Most information comes from Chapter 3, MySQL Tutorial: 1 MySQL: Part.
Intro to DatabasesClass 4 SQL REVIEW To talk to the database, you have to use SQL SQL is used by many databases, not just MySQL. SQL stands for Structured.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
2010/11 : [1]PHP with MySQLBuilding Web Applications using MySQL and PHP (W1) PHP with MySQL.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
SQlite. SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
CSC 2720 Building Web Applications Accessing MySQL from PHP.
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Introduction to Database Programming with Python Gary Stewart
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
Dept. of Computer & Information Sciences
Web Database Programming Using PHP
ASP.NET Programming with C# and SQL Server First Edition
Open Source Server Side Scripting Permissions & Users
Web Database Programming Using PHP
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
PHP Overview PHP: Hypertext Preprocessor Server-Side Scripting
ISC440: Web Programming 2 Server-side Scripting PHP 3
Chapter 8 Working with Databases and MySQL
Web DB Programming: PHP
Web Programming Language
Tutorial 6 PHP & MySQL Li Xu
PHP AND MYSQL.
Introduction to Web programming
Presentation transcript:

PHP and MySQL

Why Use a Database  Easy access to data  Simultaneous access by multiple users is handled properly  Security - easy to control access to data

Databases  A database is a collection of data organized for efficient access  A relational database is a collection of tables  Columns represent attributes  Rows represent entities  We will use MySQL for our examples

MySQL  MySQL is a free database system which is running on onyx  Standard port for MySQL is 3306  For this class, use the user name php_user and the password web2dtbs  php_user has access to the restaurant database and all databases whose names begin with test  php_user has enough privileges to create and use any database whose name has the form test_*  Please, do not drop any databases from the system except your own.

Interacting with MySQL from the shell Starting the command-line client mysql -p -u php_user Find out what databases exist show databases \g Select a database to use use databaseName \g Show all the tables in the database show tables \g Quit \q

Creating a new database  You could do this from a PHP program  Probably easier to do it manually - you don't usually want to do it more than once  Create a set of files  Define the tables  Load initial data into the tables  The directory ~tcole/teaching/cs498/demo/db on onyx has the files used to set up the restaurant database

Using MySQL from a PHP program  PHP has a collection of add-on modules called PEAR (PHP Extension and Application Repository)  One of the modules is DB which has a set of functions for interacting with databases  You write the same PHP code for any database program  Alternately, there are separate extensions for each database program (mysql for MySQL)

Using PEAR to connect  Load the DB module require 'DB.php';  Connecting  Use the connect function with a Data Source Name argument specifying host, user, database client, database  Get back an object you can use to interact with the database $db = DB::connect(

Checking for Success  The isError method in the DB module will tell yow whether you connected successfully  DB::isError($db) returns true if there was a problem  The database object has a getMessage method which provides some information  $db->getMessage()  We can set the error handling mode $db->setErrorHandling( PEAR_ERROR_DIE);

Interacting with the database  The database object has a query method that takes an SQL command as its argument  $q = $db->query( queryString)  the method returns an object  the returned object can be tested by DB::isError( $q)

Retrieving Data  Use the SQL SELECT command in the query string to retrieve data from the database $q = $db->query( 'SELECT dish_name, price FROM dishes');  The returned object gives you access to the returned data  $row = $q->fetchRow() returns either the next row (as an array) or false if there are no more  $q->numRows() returns the number of rows  $q->getAll() returns an array containing all the rows

Inserting data  Use the SQL INSERT command in your query string $q = $db->query( "INSERT INTO dishes (dish_name, price, is_spicy) VALUES ('Sesame Seed Puff', 2.50, 0)");  There are several forms for INSERT

Inserting Data from Forms  You can use form data either to create new rows or as part of the WHERE clause in a SELECT command  To make your database safe from malicious users, use placeholders to put form data into a query string $db->query( 'INSERT INTO dishes (dish_name, price) VALUES (?, ?)', array( $_POST['new_dish_name'], $_POST['new_price']));  This form of query takes care of single quotes in the entered text

Modifying data  Use the SQL UPDATE command in your query string $q = $db->query( "UPDATE dishes SET price = price * 1.1");  There are several forms for UPDATE  generally want to use a where clause to limit which rows are updated

Removing Data  Use the SQL DELETE command in your query string $q = $db->query( "DELETE FROM dishes WHERE price < 1.00");  Without the WHERE, all rows are deleted  You can check how many rows were removed with  $db->affectedRows()

MySQL without PEAR  You can use databases without using PEAR  Commands are different for each database  Use mysql (or mysqli) for MySQL databases  Which you use depends on what versions are being used; mysql seems to work on onyx

mysql Commands  Commands $db = mysql_connect( host, user, pwd, db) $q = mysql_query($db,sql) $row = mysql_fetch_row($q) msql_affected_rows( $q) mysql_num_rows( $q)  There is no automatic error handling but you can use mysql_connect_error() to check manually

Sources  Learning PHP 5 by David Sklar  Web Database Applications with PHP and MySQL by Hugh E. Williams and David Lane  PHP home page 