Connecting to Database 21 Feb 2011. Database Options Can use many different databases in conjunction with php. – MySql; MS Access; Oracle; etc etc Most.

Slides:



Advertisements
Similar presentations
Basics of Database Programming with VB6
Advertisements

ADO DB in Access VBA © Walter Milner 2005 Slide: 1 ADO VBA Programming in Access.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement.
Direct Data Access, Data Binding. Content Direct Data Access Data Binding Muzaffer DOĞAN - Anadolu University2.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Overview of Database Access in.Net Josh Bowen CIS 764-FS2008.
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 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
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.
The Tip of the ADO-burg Francisco H Tapia. Ado Objects Command Connection Error Parameter Property Record RecordSet Stream.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
PHP Data Object (PDO) Khaled Al-Sham’aa. What is PDO? PDO is a PHP extension to formalise PHP's database connections by creating a uniform interface.
3/8/00asp00 1 Active Server Pages from Microsoft Nancy McCracken Northeast Parallel Architectures Center at Syracuse.
Database 20/2/12 Connection. 
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Server Side Programming ASP1 Server Side Programming Database Integration (cont.) Internet Systems Design.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
MS Access Database Connection. Database? A database is a program that stores data and records in a structured and queryable format. The tools that are.
Elements of ASP Documents Adapted from MCDN Web Workshop ( and Webmonkey’s Introduction to Active.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 11 Accessing Database.
Web Services Week 8 Aims: –Using web services as front ends to databases Objectives: –Review of relational databases –Connecting to and querying databases.
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
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.
Relational Databases Database Driven Applications Retrieving Data Changing Data Analysing Data What is a DBMS An application that holds the data manages.
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.
CHAPTER 9 PHP AND MYSQL. A POSSIBLE SITE CONFIGURATION Application Folder index.php includes (folder)header.phpfooter.phpstyle.cssmodel (folder)mysqli_connect.php.
What is database?  Any Method for access info into Application from DataBase?  ODBC is standard for Accessing Data.  Problem with ODBC:  Information.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
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.
PHP Database Processing CIS 1715 Web Technologies.
PHP Workshop ‹#› PHP Data Object (PDO). PHP Workshop ‹#› What is PDO? PDO is a PHP extension to formalise PHP's database connections by creating a uniform.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
PHP Workshop ‹#› أطلق إبداعك 2 أطلق إبداعك 2 مدرس معتمد من مركز زووم PHP Data Object (PDO)
MySQL. Is a SQL (Structured Query Language) database server. Can be accessed using PHP with embedded SQL Queries Supports Large DB’s, 60,000 tables with.
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.
Nic Shulver, Storing Data on the Server Introduction We are going to look at some working code It writes fixed data into a simple.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 6: Accessing a database with PHP Rob Gleasure robgleasure.com.
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.
DATABASE SYSTEM VB LINK TO MS-SQL. 2 Visual ProgrammingChapter 6 Select Project -> Component.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
Using databases ActiveX Data Objects (ADO) Connecting to a database Reading data from a database Inserting, updating and deleting records Using databases.
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.
Data Control In this presentation… –databases supported –inserting a data control –data control properties –using the data control.
 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
ActiveX Data Objects (ADO)
MS Access Database Connection
ISC440: Web Programming 2 Server-side Scripting PHP 3
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Chapter 10 ADO.
Working With Databases
17 – Persistent data storage: relational databases and ADO
Presentation transcript:

Connecting to Database 21 Feb 2011

Database Options Can use many different databases in conjunction with php. – MySql; MS Access; Oracle; etc etc Most commonly used with php is MySql – Open source – Huge support in terms of online forums, tutorials, documentation and so on

Database Options We will be using MS Access Much of the material can be tweaked to suit connecting to MySql database. As per usual a good resource is w3schools.com

Create ADO connection To connect to MS Access we need to create an instance of the ActiveX Data Object (ADO) ADO are part of the component object model (COM) objects for accessing data sources //create an instance of the ADO connection object $conn = new COM('ADODB.Connection') or die('Cannot start ADO');

Connection String Create a connection string = Holds the database driver details and location of dB //define connection string variable to hold database driver details and location of dB //Make sure to change this to location of your DB $connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/inetpub/wwwroot/mbsebus/ASugrue/databases /dbtotal.accdb;Persist Security Info=False;"; // If using old access format.mdb the following connection string appropriate: $connStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\MBSEBus\ASugrue\Database s\dbtotal.mdb";

Open the Connection Pass the connection string variable (in this case $connStr) to the open function //Open the connection to the database $conn->open($connStr);

Reading from database //Code to create record set from your dB $rS = $conn->execute("SELECT * FROM tblCustomer"); $conn->execute() is used to run sql statements. $rS is created to store the record set from the sql statement

Reading from database Create variables to store the data from the record set $cust_id = $rS->Fields(0); $first_name = $rS->Fields(1);

Reading from Database Run a loop to iterate through all the records stored in the record set (as per the sql statement) while (!$rS->EOF) //while $rS is not at end of file (EOF) { echo $cust_id." ".$first_name." \n"; $rS->MoveNext(); //move to next record }

<?php //create an instance of the ADO connection object $conn = new COM('ADODB.Connection') or die('Cannot start ADO'); //define connection string variable to hold database driver details and location of dB //Make sure to change this to location of your DB $connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/inetpub/wwwroot/mbsebus/ASugrue/databases/dbtotal.accdb;Persist Security Info=False;"; // If using old access format.mdb the following connection string appropriate: $connStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\MBSEBus\ASugrue\Databases\dbtotal.mdb"; //Open the connection to the database $conn->open($connStr); //Code to create record set from your dB $rS = $conn->execute("SELECT * FROM tblCustomer"); $cust_id = $rS->Fields(0); $first_name = $rS->Fields(1); while (!$rS->EOF) { echo $cust_id." ".$first_name." "; $rS->MoveNext(); } $rS->Close(); $conn->Close(); ?>

Reading from database $rS->Close(); //release memory from rS $conn->Close(); //release memory from conn Important to close connection to database and to release memory struture of rS

Inserting a record //Code to execute sql statement to insert name John into tblCustomer field FirstName $conn->execute("INSERT INTO tblCustomer (FirstName) VALUES ('John')"); Each time this statement runs, it inserts name John into tblCustomer. Not a very useful piece of code!

Inserting a record //Code to execute sql statement to insert the value of a name variable into tblCustomer field FirstName $conn->execute("INSERT INTO tblCustomer (FirstName) VALUES (‘$name')"); Immediately more dynamic!

Insert using form input First name:

<?php $conn = new COM('ADODB.Connection') or die('Cannot start ADO'); $connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\inetpub\wwwroot\mbsebus\ASugrue\databases\dbtotal.accdb;Persist Security Info=False;"; $conn->open($connStr); $name = $_POST['name']; //Code to execute sql statement to insert name variable into tblCustomer field FirstName $conn->execute("INSERT INTO tblCustomer (FirstName) VALUES ('".$name."')"); echo "Name: ".$name." was inserted hopefully!"; $conn->Close(); ?>

Good idea to Store SQL statements in variable! <?php $conn = new COM('ADODB.Connection') or die('Cannot start ADO'); $connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\inetpub\wwwroot\mbsebus\ASugrue\databases\dbtotal.accdb ;Persist Security Info=False;"; $conn->open($connStr); $name = $_POST['name']; $conn->execute("INSERT INTO tblCustomer (FirstName) VALUES ('".$name."')"); echo "Name: ".$name." was inserted hopefully!"; $conn->Close(); ?>

Good idea to Store SQL statements in variable! <?php $conn = new COM('ADODB.Connection') or die('Cannot start ADO'); $connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\inetpub\wwwroot\mbsebus\ASugrue\databases\dbtotal.accdb;Persist Security Info=False;"; $conn->open($connStr); $name = $_POST['name']; $sqlString = “INSERT INTO tblCustomer (FirstName) VALUES ('".$name."')”; $conn->execute($sqlString); echo "Name: ".$name." was inserted hopefully!"; $conn->Close(); ?>

Update Record You might want to update data stored in the database. For instance, if someone needs to change the title or name of their project

27 Update SQL Example: $sqlString="UPDATE tblCustomer SET FirstName=‘Jim' WHERE name=‘John'";

Update Record <?php $conn = new COM('ADODB.Connection') or die('Cannot start ADO'); $connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\inetpub\wwwroot\mbsebus\ASugrue\datab ases\dbtotal.accdb;Persist Security Info=False;"; $conn->open($connStr); $sqlString="UPDATE test SET name=‘Jim’ WHERE name=‘John’"; $conn->execute($sqlString); $conn->Close(); ?>

Delete Record You may need to remove data stored in your database. For instance, the project is no longer sponsored by a company and their details must be removed from the dB

30 Delete SQL $sqlStringDelete="DELETE * FROM tblCustomer WHERE FirstName=‘Sean’";

Update Record <?php $conn = new COM('ADODB.Connection') or die('Cannot start ADO'); $connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\inetpub\wwwroot\mbsebus\ASugrue\datab ases\dbtotal.accdb;Persist Security Info=False;"; $conn->open($connStr); $sqlStringDelete="DELETE * FROM tblCustomer WHERE FirstName=‘Sean’"; $conn->execute($sqlStringDelete); $conn->Close(); ?>

For other databases -