IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 6: Accessing a database with PHP Rob Gleasure robgleasure.com.

Slides:



Advertisements
Similar presentations
Basics of Database Programming with VB6
Advertisements

Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Connecting to Database 21 Feb Database Options Can use many different databases in conjunction with php. – MySql; MS Access; Oracle; etc etc Most.
Mark Dixon Page 1 17 – Persistent data storage: relational databases and ADO.
Session 6 Server-side programming - ASP. An ASP page is an HTML page interspersed with server-side code. The.ASP extension instead of.HTM denotes server-side.
The ADO Data Control. Universal Data Access Open Database Connectivity (ODBC) –standard for accessing data in databases OLE-DB –allows access to data.
ActiveX Data Object ISYS 562. ADO An ActiveX control ActiveX is build upon COM, a contract that defines a standard interface by which objects communicate.
Database Connectivity Session 2. Topics Covered ADO Object Model Database Connection Retrieving Records Creating HTML Documents on-the-fly.
Mark Dixon Page 1 18 – Persistent data storage: relational databases and ADO.
CSE 190: Internet E-Commerce Lecture 13: Database code.
Mark Dixon Page 1 20 – Web applications: Writing data to Databases using ASP.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP.
PHP Programming. Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
What is MySQL? MySQL is a database. The data in MySQL is stored in database objects called tables. A table is a collections of related data entries and.
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
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:
ActiveX Data Object (ADO) in JavaScript J.L.Wang, Yen-Cheng Chen Dept. of Infomation Management Ming-Chuan University Jan
Database 20/2/12 Connection. 
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
Web Services Week 8 Aims: –Using web services as front ends to databases Objectives: –Review of relational databases –Connecting to and querying databases.
Mark Dixon 1 22 – Web applications: Writing data to Databases using ASP.Net.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
Copyright © 2001 by Wiley. All rights reserved. Chapter 9: Introduction to Working with Databases in Visual Basic Database Concepts Relational Database.
Tutorial 10 by Sam ine1020 Introduction to Internet Engineering 1 Database & Server-side Scripting Tutorial 10.
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.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
Lecture Note 10: Simple Database Techniques. Introduction –Database System –Access, SQL Server and others. –Microsoft Access - Interacting with this databases.
Visual Basic ADO Programming 56:150 Information System Design.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
What is database?  Any Method for access info into Application from DataBase?  ODBC is standard for Accessing Data.  Problem with ODBC:  Information.
How to Connect to Database ODBC (Open Database Connectivity) ADO (ActiveX Data Object) ASP Code To Connect to Database Recordset Object Navigating through.
ActiveX Data Objects (ADO) is Microsoft’s latest database object model. The goal of ADO is to allow VB developers to use a standard set of objects to refer.
Mark Dixon Page 1 21 – Web applications: Writing data to Databases using ASP.
AVCE ICT – Unit 7 - Programming Session 16 – Database and VB.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
Files Tutor: You will need ….
Lab 8 Data Access Using Microsoft ActiveX Data Object (ADO)
PHP Programming. Topics Database Handling (MySQL, MSSQL, ODBC)
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
Database Connectivity What is ADO. What is ADO? ADO is a Microsoft technology ADO stands for ActiveX Data Objects ADO is a Microsoft Active-X component.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 1: Introduction to IS2803 Rob Gleasure
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Source = Table rsObject.Open tablename, Connection Object, CursorType, LockType, adCmdTable Source = Stored Procedure rsObject.Open stored procedure name,
Labtest.ASP Notes. INSERT STATUS INSERT STATUS
1 ADO Activex Data Objects. 2 ADO ADO allows users to access data easily from many existing databases (such as Access or Paradox) From ODBC compliant.
Using databases ActiveX Data Objects (ADO) Connecting to a database Reading data from a database Inserting, updating and deleting records Using databases.
Generating XML Data from a Database Eugenia Fernandez IUPUI.
 2 Data Object Library approaches ◦ DAO (Data Access Objects)  Original access strategy (up to VB6)  Closely linked to MS Access ◦ ADO (ActiveX Data.
Introduction to Database Programming with Python Gary Stewart
ASP.NET Programming with C# and SQL Server First Edition
SQL – Python and Databases
The Recordset Object.
INT213 Updating the Database.
Introduction to Web programming
VISUAL BASIC INTRODUCTION TO DATA CONNECTIVITY.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Chapter 10 ASP and Data Store Access
Chapter 10 ADO.
Working With Databases
17 – Persistent data storage: relational databases and ADO
Introduction to Web programming
Presentation transcript:

IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 6: Accessing a database with PHP Rob Gleasure robgleasure.com

IS2803 Today's lecture  Accessing a database with PHP  An example

Accessing Databases with SQL As we saw in lecture 4, accessing files in PHP requires a number of steps and databases are no different. These steps (roughly) are:  Open a connection with the file  Read data from the file into a file handle variable  Manipulate the contents of the file (optional)  Close the connection

Accessing Databases with SQL Imagine the following database table leaders in politicians.accdb IDFirstnameSurname 1EndaKenny 2EamonGilmore 3MichealMartin 4GerryAdams

Accessing Databases with SQL To connect to MS Access we use the COM() function  This function creates an instance of the ActiveX Data Object (ADO), which is part of the component object model (COM) object for accessing databases and other similar data sources <?php //create an instance of the ADO connection object $conn = new COM('ADODB.Connection') or die('Cannot start ADO'); ?> Note that, just as when we were opening files in lecture 4, we are saving the connection details in a file handle (In this case ‘$conn’)

Accessing Databases with SQL Next we create a connection string to hold the database driver details and the absolute URL of the database //in this example the URL for our database is ‘C:/myFolder/politicians.accdb’ $connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:/ myFolder/politicians.accdb;Persist Security Info=False;"; Note that if we’re using the old MS Access format (.mdb), our connection string will look a little different $connStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:/xampp/htdocs/politicians.mdb"; However,.mdb issues shouldn't be a concern for us in this course

Reading from a database Next, we pass the connection string variable (in this case $connStr) to the open function //Open the connection to the database $conn->open($connStr); Again, note that we are saving all the connection details in the file handle ‘$conn’

Reading from a database Similar to the process for reading text files from lecture 4, we need to read the contents of a database into a variable When a variable is allocated the output from a SELECT query, this variable now stores what is called a ‘record set’ To run the SELECT query (or any other SQL queries) we call the ADO function ->execute() on the record set variable //Code to create a variable $rS to store a new record set from your table $rS = $conn->execute("SELECT * FROM leaders");

Reading from a database We can now pull information out of the record set variable using the ->Fields() function. $id= $rS->Fields(0); $firstname= $rS->Fields(1); $surname = $rS->Fields(2); Note that ->Fields() works on a single record in the Recordset object (i.e. one row in the table)

Reading from a database We spoke last week about pointers. ADO comes with a number of built-in functions (we have already seen open() and execute()) and a couple of these are especially important for moving a pointer (i.e. navigating) around a record set  MoveFirst (): moves to the first record in a Recordset object  MoveLast(): moves to the last record in a Recordset object  MoveNext(): moves to the next record in a Recordset object  MovePrevious(): moves to the previous record in a Recordset object Note: errors will be thrown if these functions are called and the pointer has nowhere to go, e.g. if database is empty or EOF has been reached

Reading from a database We can use these functions to iterate through the records stored in the record set while (!$rS->EOF) //while $rS is not at end of file { echo "First name is ".$rS->Fields(1)." "; // we then move to next record $rS->MoveNext(); }

Writing to a database Writing to a database is very similar, the difference is in our SQL statement //Code to execute sql statement to insert name John into leaders field FirstName $conn->execute("INSERT INTO leaders (FirstName) VALUES (6, 'Joan‘,’Collins’)"); Each time this statement runs, it will try to insert this data into a new row in the table

Closing the connection As with all files, when we’re finished we must ensure we close the connection. This closing requires two steps with databases //release memory from rS $rS->Close(); //release memory from conn $conn->Close();

An example Try out the following code Lecture 6 example <?php /**/ // create an instance of the ADO connection object $conn = new COM ("ADODB.Connection") or die("Cannot start ADO"); // define connection string, specify database driver $connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/xampp2/htdocs/IS2803.accdb;Persist Security Info=False;"; // open the connection to the database $conn->open($connStr); // create a variable to store a SQL query that selects every field where the Username is the same as $username $myQuery = "SELECT * FROM Politicians"; // create a variable $rS to store the new record returned when the SQL query is executed $rS = $conn->execute($myQuery); echo ' '; echo ' First name Surname '; echo ' '; while (!$rS->EOF){ // echo out the details from one record at a time echo ' '; echo ' '.$rS->Fields(1).' '.$rS->Fields(1).' '; echo ' '; // then move to next record $rS->MoveNext(); } echo ' '; $rS->close(); $conn->close(); ?>

Troubleshooting You may get an error that reads “Fatal error: Class 'COM' not found…” This occurs because a previously default driver isn’t default any more The solution is to open xampp/php/php.ini and add the following lines to the end [PHP_COM_DOTNET] extension=php_com_dotnet.dll

Want to read more? Links and references  W3Schools tutorial on PHP and MS Access  Generic tutorial on PHP and SQL connections Tutorial-P843.html Tutorial-P843.html