PEAR::DB fetchRow

来源:互联网 发布:浙江省dna数据库 编辑:程序博客网 时间:2024/05/14 03:29

$result->fetchRow 及 $dbh->setFetchMode

第一个例子已告诉了大家可以用 $result->fetchRow 去读取查询的结果。用 PHP 写过数据库程序的朋友都知道 PHP 提供了三种方式来传回查询: *_fetch_row*_fetch_array*_fetch_object 。 PEAR::DB 当然也不会漏了这功能。 fetchRow 的第一个参数就是所谓读取模式 (fetch mode, 结果以那种方式传回) ,可以有下列值:

  • DB_FETCHMODE_ORDERED (缺省值)
  • DB_FETCHMODE_ASSOC
  • DB_FETCHMODE_OBJECT

如没有设定读取模式,缺省就是 DB_FETCHMODE_ORDERED 。这模式 在例一已示范过了,只是把传回的结果存放在一以数字作索引的阵列(Array)的中。前后次序是根据 SELECT 命令中栏名的次序。模式 DB_FETCHMODE_ASSOC 则存放在以栏名作索引的阵列,例如:

$result = $dbh->query('SELECT id, name, mail FROM test_table');while ( $row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {    echo "ID: $row['id']<br>";    echo "Name: $row['name']<br>";    echo "E-mail: $row['mail']<br>";}

模式 DB_FETCHMODE_OBJECT 比较复杂,你最好先定义一个类别。这个类别必需要有一个只用一个阵列作参数的 constructor ,而 constructor 要从阵列中的值设回属性,如下:

 class TestUser {     var $id, $name, $mail;     function TestUser($attr = null) {         if ( is_array($attr) ) {             $this->id = $attr['id'];             $this->name = $attr['name'];             $this->mail = $attr['mail'];         }     }     function toString() {         $res = "!TestUser{/n  ID: $this->id/n";         $res .= "  Name: $this->name/n";         $res .= "  Mail: $this->mail/n}/n";         return $res;    } }

有了这个类别,我们就可以用 DB_FETCHMODE_OBJECT 去读取资料:

$dbh->setFetchMode(DB_FETCHMODE_OBJECT, 'TestUser');$result = $dbh->query('SELECT id, name, mail FROM test_table');while ( $row = $result->fetchRow()) {   echo nl2br($row->toString());}

上面的例子也首次使用了 setFetchMode 这个方法。很多人应已猜到这方法是用来设定缺省的读取模式,而如果设定的模式是 DB_FETCHMODE_OBJECT ,你就可以用第二个参数设定 fetchRow 时传回物件的类别,如上例中的 TestUser 。没有设定类别的话,fetchRow 会传回类别为 DB_row 的物件。

[编辑]

$dbh->getOne($query, $params = array())

getOne 会传回查询结果中第一笔记录的第一个栏的值。

$numOfRecord = $dbh->getOne('SELECT count(*) FROM test_table');
[编辑]

$dbh->getRow($query, $fetchmode, $params)

getRow 会传回查询结果的第一笔记录。

 $result = $dbh->getRow('SELECT * FROM test_table WHERE id = 4',     DB_FETCHMODE_ASSOC); if ( DB::isError($result) ) {    echo "Error: " . $result->getMessage();    exit(1); } echo "ID: $result['id']<br>"; echo "Name: $result['name']<br>";


[编辑]

$dbh->getCol($query, $col = 0, $params)

getRow 自然有 getCol ,作用为何也不必多说。参数 col 就是用来指定要那一栏,可以是数目字 (零代表 SELECT 命令中的第一个栏) ,也可以是栏名。不过说实,我暂时也想不到有什么理由需要这个参数。

 $result = $dbh->getCol('SELECT name FROM test_table'); if ( DB::isError($result) ) {    echo "Error: " . $result->getMessage();    exit(1); } echo "Name: " . join(', ', $result) . "<br>";
[编辑]

$dbh->getAssoc($query, $force_array)

getAssoc 会将查询的所有结果放入以第一栏为索引的阵列。例如 getAssoc('SELECT id, text FROM mydate') 的结果会是:


array (  '1' => 'one',  '2' => 'two',  '3' => 'three')

getAssoc('SELECT id, text, date FROM mydate') 的结果则是:

array (  '1' => array('one', '944'),  '2' => array('two', '944'),  '3' => array('three', '944'))
[编辑]

$dbh->getAll($query, $params, $fetchmode)

从名字都可以看出这个数可以一次过把所有查询的结果存回来 ,如 getAll('SELECT id, text FROM mydate', null, DB_FETCHMODE_ORDERED) 的结果会以下:

array(  array('1', 'one'),  array('2', 'two'),  array('3', 'three'))

getAll('SELECT id, text FROM mydate', null, DB_FETCHMODE_ASSOC) 的结果则会以下:

array(  array( 'id' => '1', 'text' => 'one'),  array( 'id' => '2', 'text' => 'two'),  array( 'id' => '3', 'text' => 'three')) 
原创粉丝点击