$ADODB_CACHE_DIR = "/var/tmp/adodb_cache"; //Directory to store cached files
$sql = "SELECT surname, age FROM employees";
$rs = &$db->CacheExecute(600,$sql); // Executes, and caches the results for 600 seconds
if (!$rs) {
print $db->ErrorMsg(); // Displays the error message if no results could be returned
}
else {
while (!$rs->EOF) {
print $rs->fields[0].' '.$rs->fields[1].'<BR>';
// fields[0] is surname, fields[1] is age
$rs->MoveNext(); // Moves to the next row
} // end while
} // end else
$sql = "SELECT surname, age FROM employees";
$rs = &$db->CacheExecute(600,$sql); // Executes, and caches the results for 600 seconds
print $rs->RecordCount() . " rows returned]"; // Display number of rows returned
$sql = "SELECT surname, age FROM employees";
$rs = &$db->CacheExecute(600,$sql); // Executes, and caches the results for 600 seconds
print $rs->FieldCount() . " columns returned]"; // Display number of rows returned
限制結果
上次我們討論了如何通過使用一個資料庫庫函數使你的應用程式更簡潔,更易於移植。在從MySQL轉移到 Informix中 , 我經曆了一次痛苦的移植過程。一切都歸咎於非ANSII標準的LIMIT子句( 舉例來說, 在MySQL中允許下列指令:SELECT name FROM employee LIMIT 15),它是一個非常有用的功能,可在Informix中卻不被支援。(在Informix中,相同功能的書寫應該是:SELECT FIRST 15 name FROM employee in Informix。)它似乎對你敲響了警鐘,要你停止在你的查詢中使用非標準SQL的指令,而去認真地學習標準的SQL。幸運的是,ADOdb有一個處理LIMIT的方法:SelectLimit()。
$sql = "SELECT surname, age FROM employees";
$rs = &$db->SelectLimit($sql, 10, 100); // Select 10 rows, starting at row 100
if (!$rs) {
print $db->ErrorMsg(); // Displays the error message if no results could be returned
}
else {
while (!$rs->EOF) {
print $rs->fields[0].' '.$rs->fields[1].'<BR>';
// fields[0] is surname, fields[1] is age
$rs->MoveNext(); // Moves to the next row
} // end while
} // end else
$sql1 = "UPDATE employees SET balance=balance-10 WHERE id=15";
$sql2 = "UPDATE employees SET balance=balance+10 WHERE id=22";
$db->StartTrans();
$db->Execute($sql);
$db->Execute($sql2);
$db->CompleteTrans();
$sql1 = "UPDATE employees SET balance=balance-10 WHERE id=15";
$sql2 = "UPDATE employees SET balance=balance+10 WHERE id=22";
$db->StartTrans();
$db->Execute($sql);
$db->Execute($sql2);
$db->CompleteTrans();
if ($db->HasFailedTrans()) {
// Something went wrong
}
值得注意的是,你的資料庫需要支援這些事務函數。 (大多數的資料庫是支援的,不過,MySQL InnoDB表支援,可 MySQL MyISAM 表不支援。)