通過(guò) Pear DB可以從查詢結(jié)果獲得更多有用的數(shù)據(jù)信息 。這些方法有: numRows(): 通過(guò)一個(gè)"SELECT" 查詢返回所有數(shù)據(jù)的數(shù)量。
3.3.1 獲取數(shù)據(jù)的函數(shù)
<?php // Once you have a valid DB Result object ... // Get each row of data on each iteration until // there is no more rows while ($row = $result->fetchRow()) { $id = $row[0]; } ?>
|
除了fetchRow()還可以使用fetchInto()直接插入$row的值。
<?php ... while ($result->fetchInto($row)) { $id = $row[0]; } ?>
|
3.3.2 選擇獲取數(shù)據(jù)的格式
獲取模式有DB_FETCHMODE_ORDERED(默認(rèn)), DB_FETCHMODE_ASSOC and DB_FETCHMODE_OBJECT.
從獲取數(shù)據(jù)方法返回的結(jié)果示例:
<?php $res = $db->query('select id, name, email from users'); $row = $res->fetchRow($mode);
//With $mode = DB_FETCHMODE_ORDERED //The default behavior is to return an ordered array. $row = array ( 0 => <column "id" data>, 1 => <column "name" data>, 2 => <column "email" data> );
$id = $row[0];
//With $mode = DB_FETCHMODE_ASSOC //Returns an associative array with column names as array keys: $row = array ( 'id' => <column "id" data>, 'name' => <column "name" data>, 'email' => <column "email" data> );
$id = $row['id'];
//With $mode = DB_FETCHMODE_OBJECT //Returns a DB_row object with column names as properties: $row = db_row Object ( [id] => <column "id" data>, [name] => <column "name" data>, [email] => <column "email" data> )
$id = $row->id; ?>
|
3.3.3 設(shè)置獲取數(shù)據(jù)的格式
可以使用 fetchrow() / fetchInto() 方法或者為你的DB實(shí)例設(shè)置一個(gè)默認(rèn)的模式。
<?php ... // 1) Set the mode per call: while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) { [..] } while ($result->fetchInto($row, DB_FETCHMODE_ASSOC)) { [..] }
// 2) Set the mode for all calls: $db = DB::connect($dsn); // this will set a default fetchmode for this Pear DB instance // (for all queries) $db->setFetchMode(DB_FETCHMODE_ASSOC); $result = $db->query(...); while ($row = $result->fetchRow()) { $id = $row['id']; } ?>
|
3.3.4 控制獲取數(shù)據(jù)數(shù)量
同時(shí)Pear DB獲取數(shù)據(jù)可以帶有額外的參數(shù),可以使用一個(gè)數(shù)字參數(shù)來(lái)獲取需要的數(shù)據(jù)數(shù)量。在你只需要獲得數(shù)據(jù)中的一部分時(shí)這時(shí)候特別有用(比如在做分頁(yè)程序的時(shí)候)
<?php ... // the row to start fetching $from = 50; // how many results per page $res_per_page = 10; // the last row to fetch for this page $to = $from + $res_per_page; foreach (range($from, $to) as $rownum) { if (!$row = $res->fetchrow($fetchmode, $rownum)) { break; } $id = $row[0]; .... } ?>
|
3.3.5 清除結(jié)果,釋放變量
當(dāng)你完成查詢的時(shí)候,可以用free()方法來(lái)結(jié)束:
<?php ... $result = $db->query('SELECT * FROM clients'); while ($row = $result->fetchRow()) { ... } $result->free(); ?>
|
3.4 快速retrieve數(shù)據(jù)
當(dāng)你不再想用fetchRow()方法來(lái)獲取數(shù)據(jù)的時(shí)候,Pear DB通過(guò)sql語(yǔ)句提供一些特別的方法來(lái)返回想要的數(shù)據(jù)。這些方法有:getOne, getRow, getCol, getAssoc and getAll. 這有一些使用示例:
<?php require_once 'DB.php'; $db = DB::connect('pgsql://postgres@unix+localhost/clients_db'); // ----------------------------------------------------------- // getOne retrieves the first result of the first column // from a query $numrows = $db->getOne('select count(id) from clients'); // ----------------------------------------------------------- // getRow will fetch the first row and return it as an array $sql = 'select name, address, phone from clients where id=1'; if (is_array($row = $db->getRow($sql))) { list($name, $address, $phone) = $row; } // ----------------------------------------------------------- // getCol will return an array with the data of the // selected column. It accepts the column number to retrieve // as the second param. // The next sentence could return for example: // $all_client_names = array('Stig', 'Jon', 'Colin'); $all_client_names = $db->getCol('select name from clients'); // ----------------------------------------------------------- // Other functions are: getAssoc() and getAll(). // For the moment refer to their in-line documentation // at pear/DB/common.php // ----------------------------------------------------------- ?>
|
"get*() 系列方法" 可以為你做很多事情, 包括: 發(fā)起一個(gè)查詢, 獲取數(shù)據(jù)和清除結(jié)果。請(qǐng)注意所有的Pear DB函數(shù)將可能返回一個(gè) Pear DB_error 對(duì)象。
3.5 從查詢結(jié)果獲得更多信息(numRows, numCols, affectedRows, tableInfo)
通過(guò) Pear DB可以從查詢結(jié)果獲得更多有用的數(shù)據(jù)信息 。這些方法有:
- numRows(): 通過(guò)一個(gè)"SELECT" 查詢返回所有數(shù)據(jù)的數(shù)量。
- numCols():通過(guò)一個(gè)"SELECT" 查詢返回所有的列。
- affectedRows(): 通過(guò)("INSERT", "UPDATE" or "DELETE")操作返回所有受影響的數(shù)據(jù)行數(shù)。
- tableInfo():通過(guò)一個(gè)"SELECT" 查詢返回一個(gè)包含數(shù)據(jù)信息的數(shù)組。
示例:
<?php ... $db = DB::connect($dsn); $sql = 'select * from clients'; $res = $db->query($sql); // Don't forget to check if the returned result from your // action is a Pear Error object. If you get a error message // like 'DB_error: database not capable', means that // your database backend doesn't support this action. // // Number of rows echo $res->numRows(); // Number of cols echo $res->numCols(); // Table Info print_r ($res->tableInfo()); // Affected rows $sql = "delete from clients"; // remember that this statement won't return a result object $db->query($sql); echo 'I have deleted ' . $db->affectedRows() . 'clients'; ?>
|
3.6 自動(dòng)增長(zhǎng)(Sequences)
Sequences 為數(shù)據(jù)行提供獨(dú)一無(wú)二的ID標(biāo)識(shí)。如果熟悉MySQL之類的話,可以把它想象為AUTO_INCREMENT.它非常簡(jiǎn)單,首先你獲取一個(gè)ID,然后在這個(gè)ID所在的行插入你所需要記錄的數(shù)據(jù)。可以為你的表設(shè)置更多的Sequences,只需要保證在任何特殊的表中都使用一樣的sequence就行。
<?php ... // Get an ID (if the sequence doesn't exist, it will be created) $id = $db->nextID('mySequence');
// Use the ID in your INSERT query $res = $db->query("INSERT INTO myTable (id,text) VALUES ($id,'foo')"); ... ?>
|
3.7 Prepare & Execute/ExcuteMultiple