Download presentation
Presentation is loading. Please wait.
Published byPosy Robertson Modified over 6 years ago
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
Example: C8ReportAll.php
Connect to DB <html> <head><title>Report inventory</title></head> <body> <h1>Reporting All Tables in myinventory</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 to the database $db_name, reason: " . mysql_error()); …
22
Example: C8ReportAll.php
Report the parts table … // Report parts $query = "select * from parts"; $result = mysql_query($query) or die("Could not query the parts table:" . mysql_error()); echo "<h2>Parts</h2>\n"; echo "<table border=1>\n"; while ($row = mysql_fetch_array($result)) { echo "<tr>\n"; echo " <td>", $row['pno']," <td>", $row['descript']," <td>", $row['price']," <td>", $row['cost'] , "</td>\n"; echo "</tr>\n"; } echo "</table>\n"; echo "<h3>Done with parts</h3>\n";
23
Example: C8ReportAll.php
Report the suppliers table … // Report suppliers $query = "select * from suppliers"; $result = mysql_query($query) or die("Could not query the suppliers table:" . mysql_error()); echo "<h2>Suppliers</h2>\n"; echo "<table border=1>\n"; while ($row = mysql_fetch_array($result)) { echo "<tr>\n"; echo " <td>", $row['sno']," <td>", $row['name']," <td>", $row['city'], "</td>\n"; echo "</tr>\n"; } echo "</table>\n"; echo "<h3>Done with suppliers</h3>\n";
24
Example: C8ReportAll.php
Report the inventory table and wrap up … // Report inventory $query = "select * from inventory"; $result = mysql_query($query) or die("Could not query the inventory table:" . mysql_error()); echo "<h2>Inventory</h2>\n"; echo "<table border=1>\n"; while ($row = mysql_fetch_array($result)) { echo "<tr>\n"; echo " <td>", $row['pno']," <td>", $row['sno']," <td>", $row['qty'], "</td>\n"; echo "</tr>\n"; } echo "</table>\n"; echo "<h3>Done with inventory</h3>\n"; ?> </body> </html>
25
Updating Data
26
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…”
27
Deleting Data
28
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
29
Example: C8UpdateInventory.php
<html> <head> <title>Update Inventory</title> </head> <body> <h1>Update inventory tables</h1> <h2>Update an existing entry to inventory</h2> <form method="POST" name= "inventory" action="C8UpdateInventory.php"> <table> <tr> <td>Part Number</td> <td>Supplier Number</td> <td>Quantity</td> </tr> <td><input type="text" name="pno" size="5"></td> <td><input type="text" name="sno" size="5"></td> <td><input type="text" name="qty" size="5"></td> </table> <p> <input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"> </p> </form> </body> </html>
30
Example: C8UpdateInventory.php
<html> <head><title>Update inventory</title></head> <body> <h1>Updating an inventory entry</h1> <?php // get the form data $pno = $_POST['pno']; $sno = $_POST['sno']; $qty = $_POST['qty']; // 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()); // Update the inventory for an existing row $update_query = "update inventory set qty = '$qty' where pno = '$pno' and sno = '$sno'"; $update_result = mysql_query($update_query) or die("Could not update inventory table:" . mysql_error()); $changed = mysql_affected_rows(); if ($changed != 1) { echo "error in updating inventory, rows changed: $changed"; } echo "<h3>Done updating inventory</h3>\n"; ?> </body> </html>
31
Sorting Data
32
Sort Use the power of SQL to: Select the data
Order the return of the data
33
Example: C8ReportParts.php (sorted by description)
<html> <head><title>Report inventory</title></head> <body> <h1>Reporting All Tables in myinventory</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 to the database $db_name, reason: " . mysql_error()); // Report parts $query = "select * from parts order by descript"; $result = mysql_query($query) or die("Could not query the parts table:" . mysql_error()); echo "<h2>Parts</h2>\n"; echo "<table border=1>\n"; while ($row = mysql_fetch_array($result)) { echo "<tr>\n"; echo " <td>", $row['pno']," <td>", $row['descript'], " <td>", $row['price']," <td>", $row['cost'] , "</td>\n"; echo "</tr>\n“; } echo "</table>\n"; echo "<h3>Done with parts</h3>\n"; ?> </body> </html>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.