PDO相当于mysql_fetch_array的 [英] PDO equivalent of mysql_fetch_array
问题描述
我的PDO相当于下面的查询挣扎其中计算有多少新项目有队列和工作了多少个星期才能完成它们,从而给我一个workstack时间表: -
I am struggling with the PDO equivalent of the following query which calculated how many new items there are in the queue and works out how many weeks to complete them, thereby giving me a workstack timescale:-
//count new to be made
$new = "SELECT FLOOR(SUM(TotalNew) / 7) AS Weeks FROM
(
SELECT YEAR( date_ready ) , MONTHNAME( date_ready ) ,
STATUS , COUNT(
STATUS ) AS TotalNew
FROM new
WHERE
(STATUS = 'new'
OR STATUS = 'progress')
GROUP BY YEAR( date_ready ) , MONTHNAME( date_ready ) ,
STATUS ORDER BY YEAR( date_ready ) , MONTH( date_ready )
) Total";
$count = mysql_query($new) or die(mysql_error());
while($row = mysql_fetch_array($count)) {
$weeks = $row['Weeks'];
}
在哪里我到是这样的....
Where I'm up to is this....
//count new to be made
$new = "SELECT FLOOR(SUM(TotalNew) / 7) AS Weeks FROM
(
SELECT YEAR( date_ready ) , MONTHNAME( date_ready ) ,
STATUS , COUNT(
STATUS ) AS TotalNew
FROM new
WHERE
(STATUS = 'new'
OR STATUS = 'progress')
GROUP BY YEAR( date_ready ) , MONTHNAME( date_ready ) ,
STATUS ORDER BY YEAR( date_ready ) , MONTH( date_ready )
) Total";
//get count data fromdb
$stmt = $dbLink->prepare($new);
$stmt->execute();
如果我想补充
$数= $ stmt->使用fetchall();
If I add $count = $stmt->fetchAll();
和转储变量,我得到
阵列(1){[0] =>阵列(2){[周] =>串(2)16[0] =>串(2)16}}
and dump the variable, I get array(1) { [0]=> array(2) { ["Weeks"]=> string(2) "16" [0]=> string(2) "16" } }
如果我更换
$count = $stmt->fetchAll();
与
while ($row = $stmt->fetchAll()) {
echo "about to print";
echo "<br>";
print_r($row);
echo "<br>";
echo $row['Weeks'];
echo "<br>";
echo "printed";
}
我得到的阵列的不同变化 - 阵列([0] =>阵列([周] => 16 [0] => 16)),但不输出I想这是从与周数查询。我试过错误的各种回声/ print_r的检查,试图看看那里的输出匹配期望的输出。
I get a different variation of the array - Array ( [0] => Array ( [Weeks] => 16 [0] => 16 ) ), but not the output I want which is the number relating to Weeks from the query. I've tried error checking with the various echo / print_r to try and see where the outputs match expected output.
查询工作正常,和原来的mysql_query版本也的作品,所以我显然误解PDO如何处理数组以及如何从阵列中拉出来的一个项目。
The query works fine, and the original mysql_query version also works, so I'm obviously misunderstanding how PDO handles arrays and how to pull an item out from within the array.
我已经看了
如何使用PDO在PHP中获取结果数组<? / A>
和
<一href=\"http://stackoverflow.com/questions/19488364/how-to-properly-use-while-loop-in-pdo-fetchall\">how在PDO使用fetchall while循环正确使用
并尝试各种组合都无济于事。说实话,即使随机的东西的工作,我想preFER知道为什么和我开始怀疑我的数组的理解。
I've looked at How to use PDO to fetch results array in PHP? and how to properly use while loop in PDO fetchAll and tried various combinations to no avail. To be honest, even if something randomly worked, I'd prefer to know why and am beginning to question my understanding of arrays.
像往常一样,我会成为一个指针或两个感激吗?
As always, I'd be grateful for a pointer or two please?
非常感谢,
杰森
Many thanks, Jason
推荐答案
我认为你正在寻找:
while($row = $stmt->fetch(/* PDO::FETCH_ASSOC */)) {
// do loop stuff
}
PDO ::使用fetchall()
返回所有查询结果的关联数组(2-D数组)。这是根据PHP文档不建议大的结果集。 PDO ::获取()
从一个结果集和模拟返回只有一行 mysql_fetch_array()
。见<一href=\"http://php.net/manual/en/function.mysql-fetch-array.php\">http://php.net/manual/en/function.mysql-fetch-array.php了解更多详情。
PDO::fetchAll()
returns an associative array of all of the query results (a 2-D array). This is not recommended for large result sets according to the PHP docs. PDO::fetch()
returns just one row from a result set and mimics mysql_fetch_array()
. See http://php.net/manual/en/function.mysql-fetch-array.php for more details.
这篇关于PDO相当于mysql_fetch_array的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!