English 中文(简体)
Zend_Db:fetchAll()或查询/fetch()
原标题:Zend_Db: fetchAll() or query()/fetch() for a huge number of records

假设我

$db is an instance of Zend_Db_Adapter_Abstract and
$sql =  SELECT blah blah FROM table  will return a huge number of records.

处理所交数据有两种代码碎片。

// Code fragment 1 (let s call it C1).
$results = $db->fetchAll($sql);
foreach ($results as $row) {
    // Process $row
}

// Code fragment 2 (let s call it C2).
$stmt = $db->query($sql);
while ($row = $stmt->fetch()) {
    // Process $row
}

我的理解是,C1将把所有已退还的数据装上美元。 因此,大量数据被装上购买力平价的记忆。 以下是我的问题。

  1. Does C2 load all data to PHP memory or does it process one by one like prepare/execute?
  2. Assuming there is no other option, is C1 or C2 a better option?

感谢!

最佳回答

你的王座是正确的。 至少如果你重新使用PDO的驾驶员,->fetch()读到未经改动的结果,而->fetchAll()将全部数据归一大阵列。

知道,如果你重新使用——并使用——fetch()的话,你必须仔细了解你试图在座右边做些什么。 你可以就同一关系提出更多的询问,而现在你还得出了无动于衷的结果。

因此,如果你计划更新 lo屋内的这些row子,那么你需要找到办法,在你离开 lo之前,推迟执行最新情况(然后做些什么)。

问题回答

To retrieve one row from the result set, use the fetch() method of the statement object. Reference

$sql =  SELECT blah blah FROM table ;
$stmt = $db->query($sql);
while ($row = $stmt->fetch()) {
    // Process $row
}

在上述例子中,stmt =$db->query($sql);检索resultset, 并使用fetch在座右铭上的现有行号resultset,该行将 cur子移至下行,直至其到达上的最后一行。

To retrieve all the rows of the result set in one step, use the fetchAll() method. This is equivalent to calling the fetch() method in a loop and returning all the rows in an array.

$sql =  SELECT blah blah FROM table ;
$stmt = $db->query($sql);
$rows = $stmt->fetchAll();
echo $rows[0][ col1 ]; // The first field/column from the first row

或者,你可以使用

....
$table = new Mytable();
// Find a single row Returns a Rowset
$rows = $table->find(1234);

// Find multiple rows Also returns a Rowset
$rows = $table->find(array(1234, 5678));

http://framework.zend.com/manual/en/zend.db.table.html。

www.un.org/spanish/ecosoc 更多:

我想fetchAll()是更快的,因为它以一个步骤检索所有数据,并回收一个阵列,但消耗更多的记忆,但fetch(<>fetch(>)消耗的记忆较少,但取而代之的是数据。

The API for fetch operations has been superseded to allow a Zend_Db_Table_Select object to modify the query. However, the deprecated usage of the fetchRow() and fetchAll() methods will continue to work without modification.

http://framework.zend.com/manual/en/zend.db.table.html





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签