Presentation is loading. Please wait.

Presentation is loading. Please wait.

Server-Side Application and Data Management IT IS 3105 (Spring 2010)

Similar presentations


Presentation on theme: "Server-Side Application and Data Management IT IS 3105 (Spring 2010)"— Presentation transcript:

1 Server-Side Application and Data Management IT IS 3105 (Spring 2010)
Lecture 20 PHP and MySQL V2

2 DB2 Reference Graeme Birchall: DB2 SQL Cookbook
Oriented towards IBM DB2 Also a good SQL reference Price is right!

3 Accessing a MySQL Database

4 Assumptions Database is on same server as the Apache server
The database has be started

5 Sample DB Tables parts inventory suppliers pno descript price cost 1
usb cable – 2 ft 1.00 .33 2 sata cable 2.00 .50 3 hdmi cable 40.00 3.33 4 power cord – 110v 5.00 .25 5 usb cable – 6 ft .66 6 power cord – 220v 6.00 7 coax – rg58 8.00 inventory pno sno qty 2 1 20 5 10 4 3 6 suppliers sno name city 1 Cables R Us Concord 2 TV Cables Kannapolis 3 All Things Power Charlotte

6 Create a connection Establish a connect to the database system of interest $connection = mysql_connect(“localhost”, “root”, “rootpw”) or die(“could not connect to db server”); Connection state is contained in $connection If there is no connection, the process terminates with the error message

7 Creating a Database

8 Creating a Database After deciding on the DB name Create the query
$newdbname = “create database if not exists myinventory”; Execute the query $new_db_result = mysql_query($newdbname) or die (“Could not create new DB: “ mysql_error());

9 Example: C8CreateDB.php
<html> <head><title>Create a Database</title></head> <body> <h1>Create a Database</h1> <h2>DB Name: myinventory</h2> <?php // connect to MySQL $connection = mysql_connect("localhost", "root", "") or die("Could not connnect to DB server"); // create a DB named myinventory $newdbname = "create database if not exists myinventory"; $new_db_result = mysql_query($newdbname) or die("Could not create new DB:" . mysql_error()); echo "<h3>Done</h3>\n"; ?> </body> </html>

10 Accessing a Database

11 To access one of the databases on MySQL
After connection select the DB to use on the DB server $db_name = “my_db_name”; $db = mysql_select_db ($db_name, $connection) or die (“Could not select the database $db_name”); Database reference is in $db

12 Creating a New Table

13 Creating a new table After defining the table characteristics
Create the query to create $c_query = “create table xyz (ent1 int, ent2 char(20), ent3 varchar(30))”; Execute the query $c_result = mysql_query($c_query) or die(“Create table failed: “ mysql_error());

14 Example: C8CreateTables.php
<html> <head><title>Create a Tables</title></head> <body> <h1>Create a set of tables</h1> <?php // connect to MySQL $connection = mysql_connect("localhost", "root", "") or die("Could not connnect to DB server"); // select the DB named myinventory $db_name = "myinventory"; $db = mysql_select_db($db_name, $connection) or die("Could not connect ot the database $db_name, reason: " . mysql_error()); // create the tables $create_query = "create table parts (pno int not null, descript varchar(80), price decimal(6,2), cost decimal(6,2), primary key (pno))"; $create_result = mysql_query($create_query) or die("Could not create new parts table:" . mysql_error()); echo "<h3>Done creating parts</h3>\n"; $create_query = "create table suppliers (sno int not null, name varchar(80), city varchar(80), primary key (sno))"; or die("Could not create new suppliers table:" . mysql_error()); echo "<h3>Done creating suppliers</h3>\n"; $create_query = "create table inventory (pno int not null, sno int not null, qty int)"; or die("Could not create new inventory table:" . mysql_error()); echo "<h3>Done creating inventory</h3>\n"; ?> </body> </html>

15 Inserting New Data

16 Inserting new data Decide on data to be inserted: Create query
$i_query = “insert into parts(pno, descript, price, cost) values(1, “usb cable – 2 ft”, 1.00, 0.33); Execute query $i_result = mysql_query($i_query) or die(“Could not insert data into table”);

17 Example: C8InsertParts.php
<html> <head><title>Insert into Parts</title></head> <body> <h1>Adding data to parts</h1> <?php // connect to MySQL $connection = mysql_connect("localhost", "root", "") or die("Could not connnect to DB server"); // select the DB named myinventory $db_name = "myinventory"; $db = mysql_select_db($db_name, $connection) or die("Could not connect ot the database $db_name, reason: " . mysql_error()); // Add a row to parts $description = "usb cable - 2 ft"; $insert_query = "insert into parts (pno, descript, price, cost) values(1, '$description', 1.00, 0.33)"; $insert_result = mysql_query($insert_query) or die("Could not insert into parts table:" . mysql_error()); echo "<h3>Done creating parts</h3>\n"; ?> </body> </html>

18 Example: retrieving data

19 Retrieving Data Decide on data to be retrieved from the DB
Create a query $my_query = “select * from parts”; Use the query to retrieve data $my_results = mysql_query($my_query); or die(“Query failed: “ . mysql_error()); All the data is returned as a “clump” in $my_results Process the data while ($data_row = mysql_fetch($my_results)) { echo $data_row; } Loop to get each row of returned data from $my_results

20 How to display data Returned results could: Go naturally into a table
Build an HTML table structure around it Be a single value Integrate in to the appropriate location in the web page

21 Updating Data

22 Updating data Decide on the data to be updated Create the query
$u_query = “update t_table set t_col = $data where condition = $value; Execute the query $u_result = mysql($u_query); or die(“Update failed: “. mysql_error()); Examine the results The “or die…”

23 Deleting Data

24 Deleting data Decide on data to be removed Create query Execute query
$value = “dog”; $d_query = “delete from d_table where d_col=$value”; Execute query $d_result = mysql_query($d_query) or die(“Delete [$d_result] failed Reason: “ . mysql_error()); Note: this deletes ALL data that matches the condition USE WITH CARE

25 Sorting Data

26 Sort Use the power of SQL to: Select the data
Order the return of the data

27 PEAR DB Module

28

29 Displaying a Table use DB data

30

31 Inserting New Data with DB

32

33 Updating Data with DB

34


Download ppt "Server-Side Application and Data Management IT IS 3105 (Spring 2010)"

Similar presentations


Ads by Google