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

时间:2016-03-13 12:23:29

标签: php mysqli

使用下面的PHP脚本时会发生以下错误:

  

第2行的/home/public_html/index.php中无法将类stdClass的对象转换为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>
    ";
    }
    }?>

1 个答案:

答案 0 :(得分:2)

要检查是否找不到结果,您需要使用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>";
}