2011-08-20 18 views
0
<?php 
#Show Recent Comments 
$rows2 = array(); 
if ($sth2->rowCount()) {          
    while($row2 = $sth2->fetch(PDO::FETCH_ASSOC)) { 
     $rows2[] = $row2; 
    } 
} 
$rows22 = array(); 
if ($sth22->rowCount()) {          
    while($row22 = $sth2->fetch(PDO::FETCH_ASSOC)) { 
     $rows22[] = $row22; 
    } 
} 


$theCommentID = $row['CommID']; 
echo "<h2 style='margin:0; padding:0;'>Recent Comments</h2>"; 
echo "<div class='comment'>by <em>{$row22['uname']}</em> on {$row2['date']} about <code><a href='course.php?cID={$row2['cID']}'>{$row2['prefix']} {$row2['code']}</a>&nbsp;</code> during {$row2['Qtr']}, {$row2['Yr']} <span style='float:right; padding-right:5px;'><img src='img/report.png' /> <a class='report' href='report.php?commID={$row2['CommID']}'>Report</a></span><br />{$row2['info']} </div>"; 


// 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)); 

// 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='$commentId'; 
"); 

$sth22->execute(array($pID22)); 

我创建的数组有什么问题吗?它们在执行查询时不会产生任何结果。Php完全没有从数据库中提取

+0

说真的。请停止在我们的代码夹住墙壁。制作[testcases](http://sscce.org/)(这是一个类似于调试的过程,这在你诉诸于此之前会很有礼貌),并且阅读manual_(你在其他地方指出,我相信它)。在你做之前,我不会再回应。 –

回答

3

那么......你先分配值,稍后再执行查询。这就像先锁门,然后试图离开你的家。你不能指望它工作。

0

除了MCHL的回答是:

$rows22 = array(); 
if ($sth22->rowCount()) {          
    while($row22 = $sth2->fetch(PDO::FETCH_ASSOC)) { 
     $rows22[] = $row22; 
    } 
} 

你的意思是:

$rows22 = array(); 
if ($sth22->rowCount()) {          
    while($row22 = $sth22->fetch(PDO::FETCH_ASSOC)) { // $sth22 here 
     $rows22[] = $row22; 
    } 
} 

而让这是一个教训,你这种风格的代码—和肯定变量的选择—是令人难以置信的错误倾向。