欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pear DB 新手入門指南教程第2/3頁

 更新時間:2008年11月21日 19:01:51   作者:  
通過 Pear DB可以從查詢結(jié)果獲得更多有用的數(shù)據(jù)信息 。這些方法有: numRows(): 通過一個"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(默認), 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 (
    => <column "id" data>,
    => <column "name" data>,
    => <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è)置一個默認的模式。

 
<?php
...
// 1) Set the mode per call:
while ($row $result->fetchRow(DB_FETCHMODE_ASSOC)) {
    [..]
}
while ($result->fetchInto($rowDB_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ù)量

同時Pear DB獲取數(shù)據(jù)可以帶有額外的參數(shù),可以使用一個數(shù)字參數(shù)來獲取需要的數(shù)據(jù)數(shù)量。在你只需要獲得數(shù)據(jù)中的一部分時這時候特別有用(比如在做分頁程序的時候)

 
<?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é)果,釋放變量

當你完成查詢的時候,可以用free()方法來結(jié)束:

 
<?php
...
$result $db->query('SELECT * FROM clients');
while ($row $result->fetchRow()) {
    ...
}
$result->free();
?>
 

 

3.4         快速retrieve數(shù)據(jù)

當你不再想用fetchRow()方法來獲取數(shù)據(jù)的時候,Pear DB通過sql語句提供一些特別的方法來返回想要的數(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ā)起一個查詢, 獲取數(shù)據(jù)和清除結(jié)果。請注意所有的Pear DB函數(shù)將可能返回一個 Pear DB_error 對象。

3.5         從查詢結(jié)果獲得更多信息(numRows, numCols, affectedRows, tableInfo)

通過 Pear DB可以從查詢結(jié)果獲得更多有用的數(shù)據(jù)信息 。這些方法有:

  • numRows(): 通過一個"SELECT" 查詢返回所有數(shù)據(jù)的數(shù)量。
  • numCols():通過一個"SELECT" 查詢返回所有的列。
  • affectedRows(): 通過("INSERT", "UPDATE" or "DELETE")操作返回所有受影響的數(shù)據(jù)行數(shù)。
  • tableInfo():通過一個"SELECT" 查詢返回一個包含數(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         自動增長(Sequences)

Sequences 為數(shù)據(jù)行提供獨一無二的ID標識。如果熟悉MySQL之類的話,可以把它想象為AUTO_INCREMENT.它非常簡單,首先你獲取一個ID,然后在這個ID所在的行插入你所需要記錄的數(shù)據(jù)??梢詾槟愕谋碓O(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

相關(guān)文章

最新評論