php pear db 没解析,Pear DB 新手入门指南教程第2/3页

3.3.1   获取数据的函数

// 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的值。

...

while ($result->fetchInto($row)) {

$id=$row[0];

}

?>

3.3.2   选择获取数据的格式

获取模式有DB_FETCHMODE_ORDERED(默认), DB_FETCHMODE_ASSOC and DB_FETCHMODE_OBJECT.

从获取数据方法返回的结果示例:

$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=> ,

1=> ,

2=> 

);

$id=$row[0];

//With $mode = DB_FETCHMODE_ASSOC

//Returns an associative array with column names as array keys:

$row= array (

'id'=> ,

'name'=> ,

'email'=> 

);

$id=$row['id'];

//With $mode = DB_FETCHMODE_OBJECT

//Returns a DB_row object with column names as properties:

$row=db_row Object

(

[id]    => ,

[name]  => ,

[email] => 

)

$id=$row->id;

?>

3.3.3   设置获取数据的格式

可以使用 fetchrow() / fetchInto() 方法或者为你的DB实例设置一个默认的模式。

...

// 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   控制获取数据数量

同时Pear DB获取数据可以带有额外的参数,可以使用一个数字参数来获取需要的数据数量。在你只需要获得数据中的一部分时这时候特别有用(比如在做分页程序的时候)

...

// 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   清除结果,释放变量

当你完成查询的时候,可以用free()方法来结束:

...

$result=$db->query('SELECT * FROM clients');

while ($row=$result->fetchRow()) {

...

}

$result->free();

?>

3.4         快速retrieve数据

当你不再想用fetchRow()方法来获取数据的时候,Pear DB通过sql语句提供一些特别的方法来返回想要的数据。这些方法有:getOne, getRow, getCol, getAssoc and getAll. 这有一些使用示例:

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*()系列方法" 可以为你做很多事情, 包括: 发起一个查询, 获取数据和清除结果。请注意所有的Pear DB函数将可能返回一个 Pear DB_error 对象。

3.5         从查询结果获得更多信息(numRows, numCols, affectedRows, tableInfo)

通过 Pear DB可以从查询结果获得更多有用的数据信息 。这些方法有:

numRows(): 通过一个"SELECT" 查询返回所有数据的数量。

numCols():通过一个"SELECT" 查询返回所有的列。

affectedRows(): 通过("INSERT", "UPDATE" or "DELETE")操作返回所有受影响的数据行数。

tableInfo():通过一个"SELECT" 查询返回一个包含数据信息的数组。

示例:

...

$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 为数据行提供独一无二的ID标识。如果熟悉MySQL之类的话,可以把它想象为AUTO_INCREMENT.它非常简单,首先你获取一个ID,然后在这个ID所在的行插入你所需要记录的数据。可以为你的表设置更多的Sequences,只需要保证在任何特殊的表中都使用一样的sequence就行。

...

// 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值