2011-08-20 27 views
-2
<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>&nbsp;</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>&nbsp;</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)) { 

两次在我上面的代码和它不让我,我不会在解封的中间代码..

+1

什么是你的问题? – pkk

+0

^请更新请求 – user886187

+1

您可以将它缩小到测试用例,而不是用所有这些PHP ..和HTML轰炸我们! –

回答

-2

你为什么不保存每一行中的阵列可以使用,只要

$rows = array(); 

if ($sth2->rowCount()) {          
    while($row = $sth2->fetch(PDO::FETCH_ASSOC)) { 
     $rows[] = $row; 
    } 
} 

,那么你可以步行到$行,只要你喜欢,并得到你想要的行,或只保存一个行,如果你只关心一个

+2

与$行= $ sth2-> fetchAll(PDO :: FETCH_ASSOC);'(不要重写内置函数,因为它们用C写) – feeela

+0

Tristan,你会发现告诉我如何用我的代码做这个?我真的很感激 – user886187

+0

@feeela是的,你可以使用fetchAll一次性完成所有工作,尽管我选择不用这个例子来尽可能接近askers的例子 – Tristan

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); 
+0

嘿菲特拉,有没有表现之间的差异呢? – user886187

+0

你能告诉我如何在我的例子中使用fetchall(),我会尝试实现它吗? – user886187

+1

“你能告诉我怎么做”要做到这一点,我需要知道你到底想要达到什么目的。另外,请阅读说明书,自己尝试一下,然后再回答具体问题。 – feeela

0

由于feeela解释,PDOStatement::fetch默认给你的“下一个”记录,所以你不能只是一味地使用它,你已经到达了结果集结束之后。

PDO接口不知道在您的PHP代码中,您突然使用新的单独的while循环来调用此函数(也不应该)。


但是,对于某些类型的结果集,可以重置光标这样你就可以在开始时重新开始。

PDOStatement::fetch的手册页解释了它:

  • PDO::FETCH_ORI_ABS为第二ARG
  • 0为第三。

请务必正确阅读本手册,因为它描述了您可以执行此操作的结果集以及如何为您的操作进行设置。


可以,而是使用PDOStatement::fetchAll检索和存储所有的记录一次,但是这是非常浪费的(在AT-A-时间内存需求而言),如果它没有明确你需要满足你的什么功能要求。


(这也将会是值得的抽穗灌浆feeela的关于您的问题WHERE话,就准备好的发言。)

+0

请用我的代码例子是最有利的。这个php网站对于初学者来说真的很糟糕 – user886187

+0

@ user886187“php网站对初学者来说真的很糟糕”什么?我无法理解,因为我了解到的关于PHP的大部分内容不是来自我大学的老师,而是来自PHP手册。这听起来像你很懒惰...... – feeela

+0

@ user886187:如果你正在努力使用PHP手册,那么答案是不能忽略它(这会让事情变得更糟),但要继续研究它,只要你不得不。跳到现实世界的例子中,只要你不知道自己在做什么,就会在脚下开枪;这就像偷汽车并沿着高速公路行驶错误的方式,因为你发现驾驶课有点困难并放弃了。 –