fetchall()之后的PDO fetchObject().返回假 [英] PDO fetchObject() after fetchall(). returning false
问题描述
我是PHP新手.
我试图以表格的形式显示员工的详细信息.
但是while($row = $result->fetchObject())
部分未执行,因为$result->fetchObject()
返回false.与$rows = $result->fetchAll();
有关吗?
这是代码段.
I am new to PHP.
I am trying to display the details of employees in form of table.
But while($row = $result->fetchObject())
part is not executing, as $result->fetchObject()
is returning false. Has it something to do with $rows = $result->fetchAll();
?
Here's the code snippet.
$sql = "SELECT id, name, designation FROM employees";
if ($result = $pdo->query($sql)) {
$rows = $result->fetchAll();
$num_rows = count($rows);
if ($num_rows > 0) {
echo "<table>\n";
echo " <tr class=\"heading\">\n";
echo " <td>ID</td>\n";
echo " <td>Name</td>\n";
echo " <td>Designation</td>\n";
echo " </tr>\n";
while($row = $result->fetchObject()) {
echo " <tr>\n";
echo " <td>" . $row->id . "</td>\n";
echo " <td>" . $row->name . "</td>\n";
echo " <td>" . $row->designation . "</td>\n";
echo " </tr>\n";
}
echo "</table>";
} else {
echo "No employees in database.";
}
else {
echo "ERROR: Could not execute $sql. " . print_r
($pdo->errorInfo());
}
推荐答案
PDO's documentation is a little confusing on this, but the PDOStatement::fetch()
method and its cousin fetchAll()
return false
when no more rows are available to return. The docs say it returns false
on failure, and a lack of available rows counts as a failure.
您对fetchAll()
的初始调用从PDOstatement
结果对象获取所有行,并且fetchObject()
调用不再需要检索,因此它返回false
.
Your initial call to fetchAll()
gets all the rows from the PDOstatement
result object and there are no more for the fetchObject()
call to retrieve so it returns false
.
您只需要初始调用fetchAll()
,但是如果您之前未为连接设置默认的提取类型,则可能需要将其提取类型设置为PDO::FETCH_OBJ
.
You only need your initial call to fetchAll()
, but you may need to set its fetch type to PDO::FETCH_OBJ
if you did not previously set the default fetch type for your connection.
然后,可以在已有的$rows
数组上用简单的foreach
循环替换while
循环.这具有将显示逻辑与数据库查询业务逻辑分开的更多好处:
Then, you can replace your while
loop with a simple foreach
loop over the $rows
array you already have. This has the added benefit of separating your display logic from your database query business logic a little more:
if ($result = $pdo->query($sql)) {
// Fetch them now, as objects
$rows = $result->fetchAll(PDO::FETCH_OBJ);
$num_rows = count($rows);
if ($num_rows > 0) {
echo "<table>\n";
echo " <tr class=\"heading\">\n";
echo " <td>ID</td>\n";
echo " <td>Name</td>\n";
echo " <td>Designation</td>\n";
echo " </tr>\n";
// $rows now has everything you need, just loop over it
foreach ($rows as $row {
echo " <tr>\n";
echo " <td>" . htmlspecialchars($row->id) . "</td>\n";
echo " <td>" . htmlspecialchars($row->name) . "</td>\n";
echo " <td>" . htmlspecialchars($row->designation) . "</td>\n";
echo " </tr>\n";
}
echo "</table>";
} else {
echo "No employees in database.";
}
else {
echo "ERROR: Could not execute $sql. " . print_r
($pdo->errorInfo());
}
还请注意,我在将输出写入HTML时向htmlspecialchars()
添加了调用.始终建议这样做,以便对< > &
这样在HTML中具有特殊含义的字符进行正确编码,如果这些值是作为用户输入产生的,则可以避免跨站点脚本漏洞.
Note also, that I added calls to htmlspecialchars()
while writing output to HTML. That is always recommended, so that characters like < > &
which have special meaning in HTML are properly encoded, and avoids cross-site scripting vulnerabilities if those values originated as user input.
这篇关于fetchall()之后的PDO fetchObject().返回假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!