最近要用到它吧,于是淡水就整理了一下
最简单的使用方式:
配置取回的结果集为关联数组
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
// 定义查询sql $query = “SELECT * FROM yourtable”; // 执行sql $result = $db->Execute($query) or die(“Error in query: $query. " . $db->ErrorMsg()); // 循环输出,注意要MoveNext(),否则死循环 while (!$result->EOF) { echo $result->fields[‘firstname’] . " - " . $result->fields[‘lastname’] . “n”; $result->MoveNext(); } // 打印结果集中的记录数 echo “n[” . $result->RecordCount() . " rows returned]n”; // 关闭连接 $db->Close(); ?> 把结果集用做对象的方法更简短一些($result->FetchNextObject()):
使用qstr转码,让“'”适应不同的DB
$title = $db->qstr(“It’s Not Me, It’s You!");
DBDate()函数来转换不同数据库之间的日期内定格式
$createdate = $db->DBDate(time());
$query = “INSERT INTO yourtable (title, createdate) VALUES ($title, $createdate)"; $result = $db->Execute($query) or die(“Error in query: $query. " . $db->ErrorMsg());
打印由 Insert_ID()取得的刚刚插入的记录的ID
if ($result) { echo “Last inserted ID is " . $db->Insert_ID(); }
$query = “DELETE FROM yourtable WHERE author = ‘J. Luser’"; $result = $db->Execute($query) or die(“Error in query: $query. " . $db->ErrorMsg());
受影响的记录数
if ($result) { echo $db->Affected_Rows() . " rows deleted”; }
// 关闭连接 $db->Close(); ?> 分页要用到的方法,SelectLimit($query,$num,$offset):
prepare query
$query = $db->Prepare(“INSERT INTO yourtable (firstname, lastname) VALUES (?, ?)");
插入的数据
$arr = array(‘wensi’,‘cater’);
执行
$result = $db->Execute($query, array($arr[0], $arr[1])) or die(“Error in query: $query. " . $db->ErrorMsg());
// 关闭连接 $db->Close(); ?> 智能事务处理(Smart Transactions):
// UPDATE
$record["firstname"] = "Caroline";
$record["lastname"] = "Smith";
# 更新了Smith的firstname
$insertSQL = $conn->AutoExecute($yourtable, $record, 'UPDATE', 'id = 1');
?>
最后修改于 2009-03-18