致命错误:未捕获错误:在布尔值[duplicate]上调用成员函数fetch_object()

致命错误:未捕获错误:在布尔值[duplicate]上调用成员函数fetch_object()

问题描述:

This question already has an answer here:

I'm trying to figure out how to fix this error... I've seen other questions that have been closed but none of them have fixed my problem, I get that there's an issue with fetch_object() being boolean but I can't figure out why.

here's the my function:

function getNews($objCon, $page = null) {
    $sql = "SELECT id, headline, description FROM cms_news";
    if ($page) {
        $sql .= "WHERE cms_category_id = $page";
    }

    $objectResult = $objCon->query($sql);
    $news = [];
    while ($row = $objectResult->fetch_object()) {
        $news[$row->id] = $row;
    }
    return $news;
}

Thank you!!!

</div>

此问题已经存在 这里有一个答案: p>

  • PHP致命错误:在布尔值上调用成员函数fetch_object() 2个答案 span> li> ul> div>

    我' 我试图弄清楚如何解决这个错误...我已经看到其他问题已经关闭,但没有一个问题解决我的问题,我得知fetch_object()有布尔的问题,但我无法想象 为什么。 p>

    这是我的函数: p>

      function getNews($ objCon,$ page = null){
     $ sql =  “SELECT id,标题,描述FROM cms_news”; 
     if($ page){
     $ sql。=“WHERE cms_categor  y_id = $ page“; 
    } 
     
     $ objectResult = $ objCon-&gt; query($ sql); 
     $ news = []; 
     while($ row = $ objectResult-&gt; fetch_object()  ){
     $ news [$ row-&gt; id] = $ row; 
    } 
    返回$ news; 
    } 
      code>  pre> 
     
     

    谢谢! !! p> div>

You need to add a space when you append your SQL:

$sql = "SELECT id, headline, description FROM cms_news ";

if ($page) {
    $sql .= "WHERE cms_category_id = $page";
}

Your previous SQL string would be:

SELECT id, headline, description FROM cms_newsWHERE cms_category_id = #