connecting to database 21 feb 2011. database options can use many different databases in conjunction...

32
Connecting to Database 21 Feb 2011

Upload: juliet-danzey

Post on 31-Mar-2015

220 views

Category:

Documents


3 download

TRANSCRIPT

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

Connecting to Database

21 Feb 2011

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

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

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

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

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

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');

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

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\Databases\dbtotal.mdb";

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

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);

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

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

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

Reading from database

• Create variables to store the data from the record set

$cust_id = $rS->Fields(0); $first_name = $rS->Fields(1);

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

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."<br />\n"; $rS->MoveNext(); //move to next record

}

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

<?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."<br/>"; $rS->MoveNext(); } $rS->Close(); $conn->Close(); ?>

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

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

Page 13: Connecting to Database 21 Feb 2011. Database Options Can use many different databases in conjunction with php. – MySql; MS Access; Oracle; etc etc Most
Page 14: Connecting to Database 21 Feb 2011. Database Options Can use many different databases in conjunction with php. – MySql; MS Access; Oracle; etc etc Most
Page 15: Connecting to Database 21 Feb 2011. Database Options Can use many different databases in conjunction with php. – MySql; MS Access; Oracle; etc etc Most
Page 16: Connecting to Database 21 Feb 2011. Database Options Can use many different databases in conjunction with php. – MySql; MS Access; Oracle; etc etc Most
Page 17: Connecting to Database 21 Feb 2011. Database Options Can use many different databases in conjunction with php. – MySql; MS Access; Oracle; etc etc Most

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!

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

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!

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

Insert using form input

<form action="insert.php" method="post">First name: <input type="text" name="name"

/><br /><input type="submit"/></form>

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

<?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(); ?>

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

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(); ?>

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

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(); ?>

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

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

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

27

Update SQL

Example:

$sqlString="UPDATE tblCustomer SET FirstName=‘Jim' WHERE name=‘John'";

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

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\databases\dbtotal.accdb;Persist Security Info=False;";

$conn->open($connStr); $sqlString="UPDATE test SET name=‘Jim’ WHERE

name=‘John’";

$conn->execute($sqlString);

$conn->Close(); ?>

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

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

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

30

Delete SQL

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

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

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\databases\dbtotal.accdb;Persist Security Info=False;";

$conn->open($connStr); $sqlStringDelete="DELETE * FROM tblCustomer WHERE

FirstName=‘Sean’";

$conn->execute($sqlStringDelete);

$conn->Close(); ?>

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

• http://www.connectionstrings.com/

For other databases -