$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 表不支持。)