23
MySQL and PHP 3 March 2006 Adina Crainiceanu www.cs.usna.edu/~adina IT420: Database Management and Organization

MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Embed Size (px)

Citation preview

Page 1: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

MySQL and PHP3 March 2006

Adina Crainiceanu

www.cs.usna.edu/~adina

IT420: Database Management and Organization

Page 2: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Web Database Architecture

Client browserWeb server with PHP enabled

Database Management System

HTTP API

Page 3: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Why Use DBMS?

Fast access to data Queries to easily extract data Built-in concurrency control Built-in security control

Page 4: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Learned So Far…

Client browserWeb server with PHP enabled

Database Management System

HTTP API

Page 5: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Goals Today

MySQL Connect from PHP to MySQL

Page 6: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

MySQL

Relational Database Management System Free Open source Portable High performance Support available

Page 7: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Working with MySQL

SQL Monitor Always available Write SQL statements ; after each statement!

PHPMyAdmin

Page 8: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Current Settings

Each machine is a server – web, db Web server: localhost:80 MySQL server: localhost:3306 MySQL user: root, no password

ALL privileges

Disk location D:/sokkit site/ mysql/

Page 9: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Lab Demo

Start MySQL server Use MySQL – local machine

Page 10: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Lab Exercise

Start MySQL server Sokkit Control Panel Start database

Start MySQL monitor D:/sokkit/mysql/bin/mysql –u root

Create a database called vp5fund create database vp5fund;

Check database was created show databases;

Page 11: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

SQL for MySQL

Surrogate keys variant: AUTO_INCREMENT

If column value left blank, generated value = max+1

show databases tables

describe tableName

Page 12: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

VP-5 MVR Fund Raiser Application

Page 13: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Lab Exercise

To use the database just created: use vp5fund;

Create table (use SQL) Items(ItemName, Price) Orders(OrderID, ShippingAddress) ItemsOrdered(OrderID, ItemName, Quantity)

Insert few rows in tables List all rows in Orders table (use SQL)

Page 14: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Example Application

Database: dbmusicTable: songs(ISBN, Title, SingerID, Length)

Page 15: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Use DBMS from PHP

Connect to the database server Specify database to use Send queries and retrieve results Process results Close connection

All PHP functions return ‘false‘ if operation unsuccessful!

Page 16: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Example: $searchterm = $_POST['searchterm'];//connect@ $db = mysql_connect('localhost','root');if (!$db){

echo('connect failed');exit;

}$dbselected= mysql_select_db('dbmusic') or exit('could not select db');//query$query = "select * from songs where Title like '%$searchterm%'";//process results$results = mysql_query($query) or die("could not retrieve rows");while ($row = mysql_fetch_row($results)){

echo 'Title: '.$row[1].' <br>';}//close connectionmysql_free_result($results);mysql_close($db);

Page 17: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Connect to MySQL

dbconnection mysql_connect(servername, username, [password])

Always test and handle errors! Example:

$dbconn = mysql_connect(‘localhost’,’root’);if (!$dbconn){

echo ‘Could not connect to db. Exit’;exit;

}

Page 18: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Select Database to Use

bool mysql_db_select(dbname, [dbconnection])

Always test and handle errors! Example:

$dbs = mysql_db_select(‘dbmusic’) ordie(‘Could not select db’);

Page 19: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Query the Database

qresult mysql_query(query) Example:

$query = “select * from songs where Title like ‘%home%’ ”;

$results = mysql_query($query);

Page 20: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Process Results

nbrows = mysql_num_rows(qresult) row = mysql_fetch_row(qresult) row = mysql_fetch_array(qresult) Example:

while ($row = mysql_fetch_row($results)){

foreach($row as $column) echo “$column ”;

echo “<br />”;

}

Page 21: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Disconnect from Database

Free query results mysql_free_result(qresult);

Close connection mysql_close(connection)

Page 22: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Lab Exercise

Save order data from VP-5 Fund Raiser application into vp5fund database. Display appropriate message in order confirmation screen.

Display all orders from VP-5 Fund Raiser application.

Display all orders from VP-5 Fund Raiser application with shipping address in Maryland.

Page 23: MySQL and PHP 3 March 2006 Adina Crainiceanu adina IT420: Database Management and Organization

Save Your Work!

Copy the D:\sokkit\mysql\data\yourdatabase directory to your X drive