本站最近一段时间在改版,如果你在浏览的过程中遇到无法访问的情况,还请见谅! [07-05-07 0:49:25]
当前位置:首 页 >> 数据库 >> MySQL >> ADODB常用操作
ADODB常用操作
作者:zpgm 发布时间:07-05-21 浏览次数:1557 [ ]

1. 连接数据库:

〈?php

include("adodb/adodb.inc.php"); # adodb的最基本的文件必须包含进去

$DB = NewADOConnection('mysql://root:1qaz2wsx@localhost/test');# 这里mysql表示连接mysql数据库,如果改成oracle就是连接oracle了,root是连接数据库用户,mysql_password是密码,localhost是主机地址,test表示数据库




$DB-〉debug=true;# 打开调试开关

if (!$DB) die("Connection failed");# 判断是否连接成功




?〉





2. 读出表纪录

〈?php

$sql = " select * from user ";

$rs = $DB-〉Execute($sql);  // 执行sql语句

//$cnt = 0;

while ($arr = $rs-〉fetchRow()) // 这里的FetchRow()相当于:mysql_fetch_array()

{

echo $arr[id].":". $arr["user"] ."〈br〉";



//print_r($arr); print "〈br〉";

}




?〉





3. 读出单条纪录:

〈?php

$row = $DB-〉GetRow("select * from user where id=21 "); // 使用GetRow()

echo $row[id] .":". $row[user]."〈br〉";




?〉




4. 读出纪录所在纪录位置:

〈?php

$val = $DB-〉GetOne("select * from user where id =22 ");

echo $val."〈br〉"; // 这里val = 22,表示第22条纪录的位置;




?〉





5. GetAll()

〈?php

$arr = $DB-〉GetAll("select * from user  where id =23 ");

foreach ($arr as $key =〉 $values)

{

    //echo $key .":". $values ."〈br〉";

    echo $values[id] .":". $values[user] ."〈br〉";

    

}




?〉





6. GetAssoc()

〈?php

$arr = $DB-〉GetAssoc("select * from user");

//print_r($arr);

echo "〈br〉";

foreach ($arr as $i =〉 $m)

{

    echo $m[id] .":". $m[user] ."〈br〉";

}




?〉





7. 使用缓存:

〈?php

$rs = $DB-〉CacheExecute(60, "select * from user where id=22 ");

while ($row = $rs-〉FetchRow())

{

    echo $row["id"] .":". $row["user"]."〈br〉";    

}




?〉





8. 

〈?php

$re = $DB-〉Execute("select * from user");

if (!$re-〉EOF)

{

    for ($i=0, $max = $re-〉FieldCount(); $i 〈 $max; $i++)

    {

        print $re-〉fields[$i].' ';

        $re-〉MoveNext();

    }

}




?〉





9. 

〈?php

$ADODB_FETCH_MODE = ADODB_FETCH_NUM;

$rs1 = $DB-〉Execute("select * from user");

$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;

$rs2 = $DB-〉Execute("select * from user");

print_r($rs1-〉fields);

foreach ($rs1-〉fields as $key =〉 $value);

{

    echo "〈br〉". $key .":". $value ."〈br〉";

}

echo "〈br〉";

print_r($rs2-〉fields);

echo "〈br〉". $rs2-〉fields['user'];




?〉


Output:

Array ( [0] => 33 [1] => N [2] => 1122475990 ) 

2:1122475990




Array ( [id] => 33 [flag_deleted] => N [user] => 1122475990 ) 

1122475990




10. 统计sql的纪录数:

〈?php

// 统计sql的纪录数

$re = $DB-〉Execute("select * from user");

$record = $re-〉RecordCount();

echo $record;




?〉





11. 获取插入后的那个id:

〈?php

// 读出最后的插入的编号;

$sql = " insert into user (user) values ('". time() ."') ";

$re = $DB-〉Execute($sql);

echo $DB-〉Insert_ID() ."〈br〉";




?〉





12. 传回被update或者insert以及delete的列数;

〈?php

$re = $DB-〉Execute("update user set flag_deleted='Y' where id〈 30 ");

echo $DB-〉Affected_Rows();// 传回被update或者insert以及delete的列数;




?〉





13. Select指令的Limit及Top支援

ADODB有個$connection->SelectLimit($sql,$nrows,$offset)函數讓你擷取recordset的部分集合,這是採用Microsoft產品中的SELECT TOP用法,及PostgreSQL與MySQL中的SELECT...LIMIT用法的優點,即使原來的資料庫並沒有提供此用法,本函數也模擬提供該使用方式。

快取支援

ADODB允許你在你的檔案系統中暫存recordset的資料,並且在$connection->CacheExecute($secs2cache,$sql)及 $connection->CacheSelectLimit($secs2cache,$sql,$nrows,$offset)等設定的時間間隔到達之後,才真正去做資料庫的查詢以節省時間。

〈?php

// select指令的limit及top支援

$sql = "  select * from user order by id asc ";

$re = $DB-〉SelectLimit($sql, 2,1);

while ($row = $re-〉FetchRow())

{

    echo $row['id'] .":". $row['user'] ."〈br〉";

}

echo "〈hr〉";

?〉

相关附件:无
相关文章[数据库驱动]:
下一篇:[07-05-21] Adodb 入门教程
上一篇:[07-05-21] ADOdb的学习资料
相关评论:
添加评论