简体   繁体   English

php PDO fetchAll() - 虽然不起作用,但foreach有效

[英]php PDO fetchAll() - while not working, foreach works

I would like to know if i'm doing fine OR fetchAll() doesn't work with WHILE. 我想知道我是否做得很好或者fetchAll()不适用于WHILE。

here is an exemple 这是一个例子

$db=new PDO("mysql:host=" .$dbhost. "; dbname=" . $dbname, $dbuser, $dbpass);

$page=$db->prepare("SELECT * FROM page");
$page->execute();

foreach ($page->fetchAll(PDO::FETCH_ASSOC) as $row) {

//echo a row
//is working
}

however, i if try looping with a while 但是,如果尝试循环一段时间我

while ($row=$page->fetchAll(PDO::FETCH_ASSOC)){

//echo a row
//Show empty
}

i tryed to use only fetch(), it was working, my question: why fetchAll() doesn't work with "WHILE" ? 我试图只使用fetch(),它正在工作,我的问题:为什么fetchAll()不能用于“WHILE”?

Fetch all returns all of the records remaining in the result set. 获取全部返回结果集中剩余的所有记录。 With this in mind your foreach is able to iterate over the result set as expected. 考虑到这一点,您的foreach能够按预期迭代结果集。

For the equivalent while implementation should use $page->fetch(PDO::FETCH_ASSOC); 对于等效的实现应该使用$page->fetch(PDO::FETCH_ASSOC);

while ($row = $page->fetch(PDO::FETCH_ASSOC)){
   // do something awesome with row
} 

if you want to use a while and fetch all you can do 如果您想使用一段时间并获取所有可以做的事情

$rows = $page->fetchAll(PDO::FETCH_ASSOC);

// use array_shift to free up the memory associated with the record as we deal with it
while($row = array_shift($rows)){
   // do something awesome with row
}

A word of warning though: fetch all will do exactly that, if the result size is large it will stress the resources on your machine. 但是请注意:获取所有内容将完全相同,如果结果大小很大,它将会对您计算机上的资源造成压力。 I would only do this if I know that the result set will be small, or I'm forcing that by applying a limit to the query. 我只会知道结果集很小,或者我通过对查询应用限制来强制执行此操作。

From the PHP Manual: 从PHP手册:

The meaning of a while statement is simple. while语句的含义很简单。 It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE. 它告诉PHP重复执行嵌套语句,只要while表达式的计算结果为TRUE即可。

Since the method you're using returns an array, while ($row=$page->fetchAll(PDO::FETCH_ASSOC)) is going to set $row to an array of the entire result set. 由于您使用的方法返回一个数组, while ($row=$page->fetchAll(PDO::FETCH_ASSOC))将$ row设置为整个结果集的数组。 What you're expecting is for the fetchAll method to extend the Iterator class , which it does not. 您期望的是fetchAll方法扩展Iterator类 ,而不是。

A foreach is the right way to go here. 一个foreach是去这里的正确方式。

no need to loop through the recordset, because fetchAll - well - fetches all the records in one command. 无需遍历记录集,因为fetchAll - 很好 - 在一个命令中获取所有记录。 Nice, isn't it? 不错,不是吗?

$rows = $page->fetchAll(PDO::FETCH_ASSOC);

// $rows is an array containing all records...
foreach ($rows as $row)
    echo $row->fieldname;

I tried to reproduce your case. 我试图重现你的情况。 Look here: 看这里:

script.php script.php的

<?php
$host = 'localhost';
$user = "user";
$password = '';
$db_name = 'test';
$port = 3306;

try
{
    $connection = new PDO("mysql:host=$host;port=$port;dbname=$db_name", $user, $password);
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
    echo 'Connection failed: ' . $e->getMessage();
}

$page=$connection->prepare("SELECT * FROM Document");
$page->execute();

while ($row = $page->fetchAll(PDO::FETCH_ASSOC)) {
    var_dump($row);
}

Database test 数据库测试

DROP TABLE IF EXISTS `Document`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `Document` (
  `DataID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Description` varchar(50) CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`DataID`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `Document`
--

LOCK TABLES `Document` WRITE;
/*!40000 ALTER TABLE `Document` DISABLE KEYS */;
INSERT INTO `Document` VALUES (1,'!!!'),(2,'This is document 2'),(3,'This is document 3'),(4,'This is document 4'),(5,'Hello');
/*!40000 ALTER TABLE `Document` ENABLE KEYS */;
UNLOCK TABLES;

output 产量

$php script.php
array(5) {
  [0]=>
  array(2) {
    ["DataID"]=>
    string(1) "1"
    ["Description"]=>
    string(3) "!!!"
  }
  [1]=>
  array(2) {
    ["DataID"]=>
    string(1) "2"
    ["Description"]=>
    string(18) "This is document 2"
  }
  [2]=>
  array(2) {
    ["DataID"]=>
    string(1) "3"
    ["Description"]=>
    string(18) "This is document 3"
  }
  [3]=>
  array(2) {
    ["DataID"]=>
    string(1) "4"
    ["Description"]=>
    string(18) "This is document 4"
  }
  [4]=>
  array(2) {
    ["DataID"]=>
    string(1) "5"
    ["Description"]=>
    string(5) "Hello"
  }
}

The output means, that while statement was executed once and prints all the rows, that the query should return, which is absolutely correct, because fetchAll returns an array of arrays with all the rows. 输出意味着,while语句执行一次并打印出查询应返回的所有行,这绝对是正确的,因为fetchAll返回包含所有行的数组数组。 PHP interprets it as true and while runs once. PHP将其解释为true并运行一次。

While foreach will iterate over the array of arrays and you will have the corresponding row every time. 虽然foreach将迭代数组数组,但每次都会有相应的行。

With fetchAll In while loop you have fetched all records in the first iteration and there is nothing to fetch the next time. 使用fetchAll In while循环,您已经在第一次迭代中获取了所有记录,并且下次没有任何内容可以获取。 In foreach also you have fetched all records in the first line, but foreach uses the result for iteration. foreach您还获取了第一行中的所有记录,但foreach使用结果进行迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM