Product Documentation

FairCom SQL for PHP PDO

Previous Topic

Next Topic

Manage

Manage() provides data management functionality for your application and/or process.

Below is the code for Manage():

//

// Manage()

//

// Populates table and perform a simple query

//

function Manage($ses) {

print("\t<h4>MANAGE</h4>\n");

if (!$ses->beginTransaction())

Handle_Error($ses, "beginTransaction()");

// populate the tables with data

Add_CustomerMaster_Records($ses);

Add_CustomerOrders_Records($ses);

Add_OrderItems_Records($ses);

Add_ItemMaster_Records($ses);

if (!$ses->commit())

Handle_Error($ses, "commit");

// perform a query:

// list customer name and total amount per order

// name total

// @@@@@@@@@@@@@ $xx.xx

// for each order in the CustomerOrders table

// fetch order number

// fetch customer number

// fetch name from CustomerMaster table based on customer number

// for each order item in OrderItems table

// fetch item quantity

// fetch item number

// fetch item price from ItemMaster table based on item number

// next

// next

$qry = $ses->query(

"SELECT cm_custname \"Name\", SUM(im_itempric * oi_quantity) \"Total\"" .

"FROM custmast, custordr, ordritem, itemmast " .

"WHERE co_custnumb = cm_custnumb AND co_ordrnumb = oi_ordrnumb AND oi_itemnumb = im_itemnumb " .

"GROUP BY co_ordrnumb, cm_custname");

if (!$qry)

Handle_Error($ses, "query(SELECT)");

else {

print("\t\t<TABLE border=1>\n\t\t\t<TR><TH>Name</TH><TH>Total</TH></TR>\n");

foreach ($qry as $row)

print "\t\t\t<TR><TD>" . $row['Name'] . "</TD><TD>" . $row['Total'] . "</TR>\n";

print("\t\t</TABLE>\n");

if ($qry->errorCode())

Handle_Error($qry, "fetch()");

$qry = null;

}

}

//

// Add_CustomerMaster_Records()

//

// This function adds records to table CustomerMaster from an

// array of strings

//

function Add_CustomerMaster_Records ($ses) {

print("\t\tAdd records in table CustomerMaster...<br>\n");

$data = array(

"('1000','92867','CA','1','Bryan Williams','2999 Regency','Orange')",

"('1001','61434','CT','1','Michael Jordan','13 Main','Harford')",

"('1002','73677','GA','1','Joshua Brown','4356 Cambridge','Atlanta')",

"('1003','10034','MO','1','Keyon Dooling','19771 Park Avenue','Columbia')"

);

Delete_Records($ses, "custmast");

foreach ($data as $values) {

$qry = $ses->exec("INSERT INTO custmast VALUES $values");

if (!$qry)

Handle_Error($ses, "exec(INSERT)");

}

}

//

// Add_CustomerOrders_Records()

//

// This function adds records to table CustomerOrders from an

// array of strings

//

function Add_CustomerOrders_Records ($ses) {

print("\t\tAdd records in table CustomerOrders...<br>\n");

$data = array(

"('09/01/2002','09/05/2002','1','1001')",

"('09/02/2002','09/06/2002','2','1002')"

);

Delete_Records($ses, "custordr");

foreach ($data as $values) {

$qry = $ses->exec("INSERT INTO custordr VALUES $values");

if (!$qry)

Handle_Error($ses, "exec(INSERT)");

}

}

//

// Add_OrderItems_Records()

//

// This function adds records to table OrderItems from an

// array of strings

//

function Add_OrderItems_Records ($ses) {

print("\t\tAdd records in table OrderItems...<br>\n");

$data = array(

"(1,2,'1','1')",

"(2,1,'1','2')",

"(3,1,'1','3')",

"(1,3,'2','3')"

);

Delete_Records($ses, "ordritem");

foreach ($data as $values) {

$qry = $ses->exec("INSERT INTO ordritem VALUES $values");

if (!$qry)

Handle_Error($ses, "exec(INSERT)");

}

}

//

// Add_ItemMaster_Records()

//

// This function adds records to table ItemMaster from an

// array of strings

//

function Add_ItemMaster_Records ($ses) {

print("\t\tAdd records in table ItemMaster...<br>\n");

$data = array(

"(10,19.95,'1','Hammer')",

"(3, 9.99,'2','Wrench')",

"(4, 16.59,'3','Saw')",

"(1, 3.98,'4','Pliers')"

);

Delete_Records($ses, "itemmast");

foreach ($data as $values) {

$qry = $ses->exec("INSERT INTO itemmast VALUES $values");

if (!$qry)

Handle_Error($ses, "exec(INSERT)");

}

}

//

// Delete_Records()

//

// This function deletes all the records in a tables

//

function Delete_Records ($ses, $table) {

print("\t\tDelete records...<br>\n");

$qry = $ses->exec("DELETE FROM $table");

if (!$qry)

if (0000 == $ses->errorCode())

return;

else

Handle_Error($ses, "exec(DELETE)");

}

TOCIndex