MySQL and PHP By Trevor Adams.

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

Basic SQL Introduction Presented by: Madhuri Bhogadi.
Mysql Mohammed M. Hassoun Introducing Databases and SQL In the Internet age, information is no longer represented in filing cabinets; instead, it’s.
Introduction to Structured Query Language (SQL)
Objectives Connect to MySQL from PHP
Introduction to Structured Query Language (SQL)
CSC 2720 Building Web Applications Database and SQL.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Database Lecture # 1 By Ubaid Ullah.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
7/8/05MySQL David Lawrence1 David Lawrence, JLab An introduction for the novice.
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 &
ASP.NET Programming with C# and SQL Server First Edition
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
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)
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Introduction to SEQUEL. What is SEQUEL? Acronym for Structural English Query Language Acronym for Structural English Query Language Standard language.
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.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Lecture 10 – MYSQL and PHP (Part 2)
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
MySQL Database Connection
Session 7: Getting PHP to Talk to MySQL. Objectives Connecting to the Database Building & Executing the SQL SELECT Query Fetching & Displaying the data.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
PHP Database connectivity Connecting with RDBMS and editing, adding, and deleting databases therein are all done through PHP functions.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Information Building and Retrieval Using MySQL Track 3 : Basic Course in Database.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
Module Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
SQL.. AN OVERVIEW lecture3 1. Overview of SQL 2  Query: allow questions to be asked of the data and display only the information required. It can include.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
1 A Very Brief Introduction to Relational Databases.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
SQL Introduction to database and SQL. Chapter 1: Databases and Database Users 6 Introduction to Databases Databases touch all aspects of our lives. Examples:
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
SQL Basics Review Reviewing what we’ve learned so far…….
Introduction to Database Programming with Python Gary Stewart
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Introduction to Structured Query Language(SQL)
Chapter 8 Working with Databases and MySQL
SQL .. An overview lecture3.
Introduction To Structured Query Language (SQL)
Presentation transcript:

MySQL and PHP By Trevor Adams

Topics Covered What is SQL? SQL Standards MySQL MySQL and PHP Database Tables Queries Data Manipulation Language (DML) Data Definition Language (DDL) MySQL and PHP Connecting to MySQL Server Using a connection Functions

What is SQL? Structured Query Language Allows database operations Retrieve data Modify data Insert new data Remove data Create and modify tables English type syntax

SQL Standards ANSI (American National Standards Institute) SQL is available on many platforms and products Many products implement specific features that are exclusive A product must meet the requirements of ANSI SQL to be considered ANSI SQL compliant Assists programmers by using a common syntax

MySQL Available as both a commercial and open-source product Implements the SQL standards Available on many platforms Windows Linux Mac Unix Available from http://www.mysql.com/

MySQL - Database A MySQL server is capable of storing many databases A database is generally made of a collection of related tables Each student will get one database for use with the module Every database will be accessible by the student that owns it

MySQL - Tables A database is generally made up of related tables Each table will have a name that is unique within the database A table contains records with data StudentID Forename Surname Level AK123900 Trevor Adams M AB340948 Bobby Ratchet 3 AC234887 Johan Doex 2

MySQL - Queries StudentID AK123900 AB340948 AC234887 A query performed on a database can result in data or some kind of status A returned list of required records Whether a deletion was successful SELECT StudentID FROM Student Returns a result set StudentID AK123900 AB340948 AC234887

MySQL - Queries Queries can come in the following forms: SELECT – extracting data UPDATE – updates data DELETE – deletes data INSERT – inserts data All of these queries can be used on the MySQL database software

Data Manipulation Language Consists of the queries that enable the developer to modify the data contained The SQL server processes these queries and returns a result set or a status notification

Data Definition Language Defines a set of queries that can be used by the developer to modify the data structure CREATE TABLE ALTER TABLE DROP TABLE CREATE INDEX DROP INDEX We shall not be covering these commands to a great extent Use a management tool to generate these commands automatically

PHP and MySQL PHP contains all of the functionality required to interact with MySQL servers Most PHP MySQL functions are prefixed with ‘mysql_’ Use the PHP homepage to search for mysql_ and examine the results MySQL is a client-server based DBMS Database management system One (or few) server(s) caters for many clients Possible for web server and DBMS server to be on the same system

Connecting to MySQL with PHP Use the MySQL connect routine mysql_connect($host, $user, $password) $user and $password will be your account details mysql_connect will return a link ID $link = mysql_connect($host, $user, $password) if(!$link) { echo “Unable to connect”; } Always check the link to ensure that the database connection was successful

Selecting a database Once a link has been established, select a database mysql_select_db($dbname, [$link]) [] optional – uses last created $link if not given mysql_select_db returns a Boolean indicating status $result = mysql_select_db(“students”) If(!$result) { echo “No database”; }

Using a Connection Once a connection has been established it is possible to execute queries Queries always return a result Success status Result Set Use mysql_query($query_string) to execute $query = “SELECT * FROM Students”; $result = mysql_query($query); $result will contain the desired result set or false if not available

Using a Connection Use functions mysql_fetch_row($result) to obtain a row from the result set Returns false when no rows left Example: $query = “SELECT * FROM Students”; $result = mysql_query($query); While($row = mysql_fetch_row($result)){ // $row will be an array index at 0 per column } $row will be equal to false (ending the while loop) when there are no more rows left

SQL Query Types SELECT SELECT [fields,…] FROM [table] WHERE [criteria] ORDER BY [field] [asc,desc] [fields] can be * for all or field names separated by commas [table] is the name of the table to use [criteria] is a collection of Boolean expressions that limits returned rows E.g. Forename=‘Trevor’ AND Surname=‘Adams’ [field] denotes which field to sort by

SQL Query Types INSERT INTO INSERT INTO [table]([fields,…] VALUES([newvalues,…]) [table] indicates which table to insert into [fields] is a comma separated list of fields that are being used [newvalues] is comma separated list of values that directly correspond to the [fields] E.g. INSERT INTO students(StudentID, Surname, Forename, Level) VALUES(‘AK301390’, Adams, Trevor, M)

SQL Query Types UPDATE [table] denotes the table to update UPDATE [table] SET [field=value,…] WHERE [criteria] [table] denotes the table to update [field=value,…] is a comma separated list of values for fields [criteria] – a Boolean expression that specifies which records to update If no criteria is given, all records would be updated UPDATE students SET forename=‘Trevor’ WHERE StudentID=‘AK301390’ With no where clause every record in the table would be updated with forename=‘Trevor’

SQL Query Types DELETE Simple and dangerous statements DELETE FROM [table] WHERE [criteria] Simple and dangerous statements [table] to delete from [criteria] specifying records to delete No criteria deletes all records DELETE FROM students Removes all student records with no warning and no sympathy for mistakes. E.g. DELETE FROM students WHERE StudentID=‘AK301390’ Deletes the student with StudentID of ‘AK301390’

Quick Example $query = “INSERT INTO students (StudentID, Forename, Surname, Level) VALUES (‘AK301390’, ‘Trevor’, ‘Addams’, ‘M’) $result = mysql_query($query); if(!$result) { Echo “Insertion failed”; } else { Echo “Record inserted”; }

Quick Example $query = “UPDATE students SET Surname=‘Adams’ WHERE StudentID=‘AK301390’ $result = mysql_query($query); If(!$result) { echo “Update failed!”; } else { echo “Update successful!”; }

Quick Example $query = “SELECT * FROM student”; $result = mysql_query($query); If(!$result) { echo “No result set”; } else { while ($row = mysql_fetch_row($result)){ foreach($row as $value){ echo “$value, ”; }

Useful functions Many mysql functions can take a link identifier but do not need it. Simply uses the last one opened mysql_affected_rows ( [link_identifier] ) Returns the number of rows affected from the last query mysql_errno ( [link_identifier] ) Gets the last error number from the server mysql_error ( [link_identifier] ) Returns a string containing error information mysql_fetch_array (result [,result_type] ) Retrieves a record from a result set as an array, optional result time can be MYSQL_ASSOC, MYSQL_NUM or default MYSQL_BOTH.

Other Resources SQL is a big topic, a walk-through tutorial will be available in the lab session Meanwhile – take a look at the following resources: http://www.w3schools.com/sql/default.asp http://www.php.net/mysql You can obtain MySQL free of charge from http://dev.mysql.com/downloads/ Version 5.x has just been released University currently uses 4.x