类 stdClass 的对象无法在第 2 行的 /home/public_html/index.php 中转换为 int


Object of class stdClass could not be converted to int in /home/public_html/index.php on line 2

使用以下 PHP 脚本时会出现以下错误:

类 stdClass 的对象无法在第 2 行的/home/public_html/index.php 中转换为 int

<?
while($Recents = mysqli_fetch_object($RecentForums)){
    if($Recents == 0){
    echo"
        <center>
            No recent forum posts!
        </center>
        ";
    }
else{
    $getPoster = mysqli_query($con, "SELECT * FROM Users WHERE ID='".$Recents->PosterID."'");
    $gP = mysqli_fetch_object($getPoster);
    echo"
    <img src='http://sitelink.com/Images/BulletPointArrow.png'>
    &nbsp;
    <a href='http://sitelink.com/forum/ShowPost.php?ID=".$Recents->ID."'>
    ".$Recents->Title."
    </a>
    <br />
    <font style='font-size:11px'>
    by 
    <a href='http://http://sitelink.com/user.php?ID=".$gP->ID."' style='font-size:11px;'>
    ".$gP->Username."
    </a>
    on ".$Recents->TimePosted."
    </font>
    <div style='margin-top:7px;'></div>
    ";
    }
    }?>

要检查是否未找到结果,您需要使用 mysqli_num_rows()

if( mysqli_num_rows($RecentForums) == 0 ) {
    echo "
        <center>
            No recent forum posts!
        </center>";
    }
}

或者,如果您需要显示结果 - 您可以执行以下操作:

$recordsFound = false;
while( $Recents = mysqli_fetch_object($RecentForums) ){
    $recordsFound = true;
    echo $Recents->Title . "'n";
}
if( !$recordsFound ) {
    echo "<center>
        No recent forum posts!
    </center>";
}