pear mdb2 使用範例query,queryOne,queryRow,queryAll
用簡單的範例,來記錄mdb2套件的函式query、queryOne、queryRow、queryAll從資料庫取出資料後,要怎麼把它給呈現出來:
$dbconn是自行建立的資料庫連結的物件,請先瀏覽此文pear mdb2 使用教學 資料庫連結
1. query
$dbquery = $dbconn->query("select ID,name,birthday from member"); while ($data = $dbquery->fetchRow()) { echo $data['ID'].','.$data['name'].','.$data['birthday']; }
2. queryOne
$dbquery = $dbconn->queryOne("select count(*) from member"); if (!is_null($dbquery)) echo $dbquery;
3. queryRow
$dbquery = $dbconn->queryRow("select name,birthday from member where ID = 1"); if (!is_null($dbquery)) echo $dbquery['name'].','.$dbquery['birthday'];
4. queryAll
$dbquery = $dbconn->queryAll("select ID,name,birthday from member"); foreach ($dbquery as $data) { echo $data['ID'].','.$data['name'].','.$data['birthday']; }
註:query也可用來執行insert,update,delete等sql敘述,但建議使用exec
參考文:PEAR的MDB2使用心得