<div class="professor_comments">
<?php
#Show User Who Submitted Content
if ($sth2->rowCount()) {
while($row = $sth2->fetch(PDO::FETCH_ASSOC)) {
$theCommentID = $row['CommID'];
}
}
else {
echo "No CommID";
}
echo "Value of CommID: $theCommentID";
echo "<h2 style='margin:0; padding:0;'>Recent Comments</h2>";
if ($sth22->rowCount()) {
while($row = $sth22->fetch(PDO::FETCH_ASSOC)) {
echo "<div class='comment'>by <em>{$row['uname']}</em>";
}
}
else {
echo "User";
}
unset($sth22);
#Show Recent Comments
if ($sth2->rowCount()) {
while($row = $sth2->fetch(PDO::FETCH_ASSOC)) {
echo "on {$row['date']} about <code><a href='course.php?cID={$row['cID']}'>{$row['prefix']} {$row['code']}</a> </code> during {$row['Qtr']}, {$row['Yr']} <span style='float:right; padding-right:5px;'><img src='img/report.png' />
<a class='report' href='report.php?commID={$row['CommID']}'>Report</a></span><br />{$row['info']} </div>";
}
}
else {
echo "<h2 style='color:red;'> No Comments Found, please add some below</div>";
}
unset($sth2);
?>
<?php
// Get any professor comments currently present ON LOAD
$pID2 = filter_input(INPUT_GET, 'pID', FILTER_SANITIZE_NUMBER_INT);
$pdo2 = new PDO('mysql:host=host;dbname=dbname', $u, $p);
$pdo2->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth2 = $pdo2->prepare('
SELECT C.cID, Co.CommID, prefix, code, info, date, Qtr, Yr
FROM Course C, Comment Co, Professor P
WHERE P.pID = ?
AND C.cID = Co.CName AND P.pID = Co.pID
ORDER BY Yr DESC;
');
$sth2->execute(array(
$pID2
));
?>
PHP脚本
<?php
// Get the user of the comment
$pID22 = filter_input(INPUT_GET, 'pID', FILTER_SANITIZE_NUMBER_INT);
$pdo22 = new PDO('mysql:host=host;dbname=dbname', $u, $p);
$pdo22->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth22 = $pdo22->prepare("
SELECT uname FROM Student S, Comment C WHERE S.usrID = C.usrID and commID='$theCommentID';
");
$sth22->execute(array(
$pID22
));
?>
This gives me the inital value of the commID outputted, but then seems to ignore all fetching for rows when :
#Show Recent Comments
if ($sth2->rowCount()) {
while($row = $sth2->fetch(PDO::FETCH_ASSOC)) {
is called again. Why is it only outputted the first `commID` but then omitting this second call for `$sth2` :
#Show Recent Comments
if ($sth2->rowCount()) {
while($row = $sth2->fetch(PDO::FETCH_ASSOC)) {
echo "on {$row['date']} about <code><a href='course.php?cID={$row['cID']}'>{$row['prefix']} {$row['code']}</a> </code> during {$row['Qtr']}, {$row['Yr']} <span style='float:right; padding-right:5px;'><img src='img/report.png' />
<a class='report' href='report.php?commID={$row['CommID']}'>Report</a></span><br />{$row['info']} </div>";
}
}
else {
echo "<h2 style='color:red;'> No Comments Found, please add some below</div>";
}
unset($sth2);
问题:我正在使用:
if ($sth2->rowCount()) {
while($row = $sth2->fetch(PDO::FETCH_ASSOC)) {
我上面的代码中有两次,它不让我和我在中间解开代码..
答案 0 :(得分:3)
PDOStatement->fetch()
Fetches the next row from a result set,因此您无法再次使用它来检索相同的值。使用PDOStatement->fetchAll()
将所有结果提取到单个数组中,可以重复使用。
你的WHERE子句中也有commID='$theCommentID'
,看起来这个语句应该只返回一行 - 如果是这样的话,就不需要循环了。
$firstRow = $sth2->fetch(PDO::FETCH_ASSOC);
/* $secondRow = $sth2->fetch(PDO::FETCH_ASSOC); */
第三,使用PDO但不使用参数绑定,与保存数据库访问的整个想法相反。
// instead
$sth22 = $pdo22->prepare("SELECT uname FROM Student S, Comment C WHERE S.usrID = C.usrID and commID='$theCommentID';");
// better use something like
$sth22 = $pdo22->prepare('SELECT uname FROM Student S, Comment C WHERE S.usrID = C.usrID and commID=:comment_id');
$sth22->bindValue(':comment_id', $theCommentID);
答案 1 :(得分:0)
正如费耶拉所解释的那样,默认情况下PDOStatement::fetch
会为您提供“下一条”记录,因此您不能在结束集结束后继续使用它。
PDO接口并不知道,在您的PHP代码中,您突然使用一个新的独立while
循环来调用此函数(也不应该)。
但是,对于某些类型的结果集,可以重置光标,以便您可以在开头重新开始。
PDOStatement::fetch
的手册页解释了它:
PDO::FETCH_ORI_ABS
for 2nd arg 0
获得第3名。请务必正确阅读本手册,因为它描述了您可以执行此操作的结果集,以及如何为您自己完成此操作。
相反,您可以使用PDOStatement::fetchAll
一次检索和存储所有记录,但如果没有明确说明您需要满足您的需求,则这非常浪费(就一次性内存要求而言)功能要求。
(对于你的WHERE
问题以及准备好的陈述,听取费拉的话也是值得的。)
答案 2 :(得分:-2)
为什么不保存数组中的每一行,只要
就可以使用类似
$rows = array();
if ($sth2->rowCount()) {
while($row = $sth2->fetch(PDO::FETCH_ASSOC)) {
$rows[] = $row;
}
}
然后你可以随时遍历$ rows并获得你想要的行,或者如果你只对一行感兴趣,只需保存一行