Php从DB中完全没有任何东西

时间:2011-08-20 14:42:10

标签: php mysql

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

我正在创建的阵列出了什么问题?它们在执行查询时不会产生任何结果。

2 个答案:

答案 0 :(得分:3)

嗯......你先分配值,稍后再执行查询。这就像首先锁上你的门,然后试图离开你的家。你不能指望它能够发挥作用。

答案 1 :(得分: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;
    }
} 

让这一点成为你的一个教训,这种代码风格 - 以及当然你选择的变量 - 非常容易出错。