- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在循环中输出myTable的编号并输出myTable的field1。
但是,如果我运行以下代码,只会打印 10 个“test :”。
如果我删除 [$rows = $statement->fetchAll(PDO::FETCH_ASSOC);]
并将 for() 的 $count
更改为 10
,它工作正常,但这不是我想要的。
<?php
$db = new PDO("mysql:host=localhost;dbname=mydb;charset=utf8;", myid, mypw);
$statement = $db->query('select field1 from myTable limit 10');
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
$count = count($rows);
echo "count : $count<br><br>";
for($i = 0; $i < $count; $i++)
{
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo 'test : '.$row['field1'].'<br>';
}
?>
我想避免使用以下方法来计算 myTable 的数量。因为不必要地添加了看起来相似的查询语句。
$count = $db->query('SELECT count(*) FROM qudtls_mutter')->fetchColumn();
最佳答案
结果,来自
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
将是关联数组,您需要对其进行计数。
您可以简单地使用 $statement->rowCount():int
这将返回上次查询的计数。
然后你可以将它与FOR
混合使用
for ($i=0; $i<$statement->rowCount(); $i++) {
}
对于其他用途,您可以使用WHILE
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
}
要使用foreach
,只需:
foreach ($statment as $row) {
}
关于php - 为什么 fetch_all 语句之后的 fetch 语句没有任何值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44816774/
仍然是 PHP 新手。我将 PHP 版本 5.6.2 与 MAMP 一起使用。我正在尝试获取所有表条目。代码是: $query = "SELECT * FROM atable"; $result =
我需要替换 fetch_all,因为我的主机不支持它 找到了用循环来实现的方法。但我不能。 函数 function getMenusuno() { global $conex; $resp
我已经设置了一个类来处理我的 MySQLi 调用。调用正常工作,因此没有问题。 我最近将结果处理从类的查询方法中删除为两个单独的 setter 方法。现在,两次连续的 fetch_all 调用会导致错
感谢您的回答,我发现我无法使用 fetch_all() 因为我正在使用 PHP 5.2.17 - fetch_assoc > 与 while 循环有效。 我正在使用的函数fetch_all返回时出现以
请羞辱我。这里有什么不对?我希望像 ->fetch_all(Opt) 这样的东西,一个单行,将所有结果放在一个数组中,但无法使其工作。这就是我最终要做的: $s = "select id, us
这可能会被标记为重复,但我的错误似乎与我在 stackoverflow 上看到的其他错误不同。 下面的代码应该显示图像中的内容:代码: if($news_result = $db->query("S
感谢答案,我发现我无法使用 fetch_all() 因为我使用的是 PHP 5.2.17 - fetch_assoc while 循环工作。 我正在使用的函数 fetch_all 返回此错误: Fat
感谢答案,我发现我无法使用 fetch_all() 因为我使用的是 PHP 5.2.17 - fetch_assoc while 循环工作。 我正在使用的函数 fetch_all 返回此错误: Fat
我正在尝试创建一个 JSON 回调。我有两个文件,json.html 和 json.php。另外,我有一个这样的数据库: Type: MySQL DB Name: user_table Table n
这个问题已经有答案了: PHP mySQLi_fetch_all: iterate through each row (4 个回答) 已关闭 9 年前。 我有一个数据库,其中列出了我的所有用户,并且与
我制作了从数据库获取数据表的方法,我正在使用 MySQL和PHP ver. 5.6 ,这是我的代码: public function fetch($table, $notation = null, $
我正在尝试让 fetch_all 函数与 mysqli 一起使用。我知道我需要 PHP > 5.3 和 mysqlnd。 我的服务器运行 Debian Wheezy,所以我有 PHP 5.5。我刚刚删
如何使用 fetch_assoc 将 fetch_all 的输出与循环精确匹配?我围绕 fetch_all 构建了代码,但我的 PHP 5.4 Bluehost 服务器不足以运行它(我正在与他们讨论)
不太清楚为什么,但我今天已经多次看到这种情况。 global $connection; $sql = "SELECT * FROM table"; $result = $connection->que
我想在循环中输出myTable的编号并输出myTable的field1。 但是,如果我运行以下代码,只会打印 10 个“test :”。 如果我删除 [$rows = $statement->fetc
是否可以使用 MySQLi_Result 类中的 fetch_all()、fetch_assoc() 等以及来自MySQLi_STMT 类? 我使用 Mysqli 类中的 query() 方法(并通过
我已将此问题分解为本质,但仍有问题。 当我尝试使用 fetch_all 获取存储过程的结果时,我得到的结果按预期返回到数组,但随后的 mysqli 调用抛出“命令不同步”错误。 我什至尝试将我的存储过
我在 Ubuntu 10.04 中遇到 PHP 问题。当我尝试使用 mysqli_result::fetch_all 时出现此错误: 调用未定义方法 mysqli_result::fetch_all(
严格从 MySQL 的角度(数据库性能,而不是 PHP 性能)来看,在检索查询结果时,Mysqli fetch_assoc() 循环与 Mysqli fetch_all() 之间有什么区别? 假设 $
它们是mysqli_result的两个函数。 fetch_array():获取结果行。 这样,查询每一行到数据库? fetch_all():获取所有结果行。 而且这样只查询一次数据库? 我不知道如何以
我是一名优秀的程序员,十分优秀!