函数接受订单 ID 并返回一个二维数组,该数组包含表示订单的订单项的行。
清单 5. 获取指定订单的订单项
复制代码 代码如下:
<?php
//File:getOrderItems.php
require_once ''connect.php'';
function getOrderItems($order_no) {
if (!$rsConnection = GetConnection()){
return false;
}
$strSQL = "SELECT * FROM ORDER_ITEMS WHERE
order_id =:order_no ORDER BY line_item_id";
$rsStatement = oci_parse($rsConnection,$strSQL);
oci_bind_by_name($rsStatement, ":order_no", $order_no, 12);
if (!oci_execute($rsStatement)) {
$err = oci_error();
trigger_error(''Query failed:'' . $err[''message'']);
return false;
}
$nrows = oci_fetch_all($rsStatement, $results);
return array ($nrows, $results);
}
?>
注意,以上两个函数都需要 connect.php 脚本,该脚本应包含返回数据库连接的 GetConnection 函数。清单 6 就是 connect.php 脚本:
清单 6. 获取数据库连接
复制代码 代码如下:
<?php
//File:connect.php
function GetConnection() {
$dbHost = "dbserverhost";
$dbHostPort="1521";
$dbServiceName = "orclR2";
$usr = "oe";
$pswd = "oe";
$dbConnStr = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$dbHost.")
(PORT=".$dbHostPort."))(CONNECT_DATA=(SERVICE_NAME=".$dbServiceName.")))";
if(!$dbConn = oci_connect($usr,$pswd,$dbConnStr)) {
$err = oci_error();
trigger_error(''Failed to connect '' .$err[''message'']);
return false;
}
return $dbConn;
}
?>
现在,您已经创建了与数据库通信所需的所有函数,下面我们将了解一下 Cache_Lite_Function 类的工作方式。清单 7 是 testCache.php 脚本,该脚本使用 Cache_Lite_Function 类缓存以上函数的结果。
清单 7. 使用 PEAR::Cache_Lite 缓存
复制代码 代码如下:
<?php
//File:testCache.php
require_once ''getOrderItems.php'';
require_once ''getOrderFields.php'';
require_once ''Cache/Lite/Function.php'';
$options = array(
''cacheDir'' => ''/tmp/'',
''lifeTime'' => 86400
);
if (!isset(
通过缓存数据库结果提高PHP性能的原理介绍_网学
浏览:
GET[''order_no''])) {
die(''The order_no parameter is required'');
}
$order_no=
通过缓存数据库结果提高PHP性能的原理介绍_网学
浏览:
GET[''order_no''];
$cache = new Cache_Lite_Function($options);
if ($orderfields = $cache->call(''getOrderFields'', $order_no)){
print "<h3>ORDER #$order_no</h3>\n";
print "<table>";
print "<tr><td>DATE:</td><td>".$orderfields[''ORDER_DATE'']."</td></tr>";
print "<tr><td>CUST_ID:</td><td>".$orderfields[''CUSTOMER_ID'']."</td></tr>";
print "<tr><td>TOTAL:</td><td>".$orderfields[''ORDER_TOTAL'']."</td></tr>";
print "</table>";
} else {
print "Some problem occurred while getting order fields!\n";
$cache->drop(''getOrderFields'', $order_no);
}
if (list($nrows, $orderitems) = $cache->call(''getOrderItems'', $order_no)){
//print "<h3>LINE ITEMS IN ORDER #$order_no</h3>";
print "<table border=1>";
print "<tr>\n";
while (list($key, $value) = each($orderitems)) {
print "<th>$key</th>\n";
}
print "</tr>\n";
for ($i = 0; $i < $nrows; $i++) {
print "<tr>";
print "<td>".$orderitems[''ORDER_ID''][$i]."</td>";