download20151010 2018-06-02 06:10
浏览 59
已采纳

未捕获错误:调用成员函数错误消息

UPDATE 1:

I forgot to add GetTags() method, so here it is:

public $blog_tags;
public function GetTags()
{
    return $this->blog_tags;
}

=========================================================================

I'm working with PHP OOP to develop my project. Basically I have a table called blogs which contains some fields and data like this image:

capture

Then I created a class Blos.class.php and made two methods as below:

public function ShowTag()
{
    $tag = $this->_db->prepare("SELECT blog_tags FROM blogs");
    $tag->execute();
    while($row = $tag->fetch())
    {
        $this->blog_tags = $row['blog_tags'];
    }
}
public function NumTag()
{
    $cat = $this->_db->prepare("SELECT blog_tags FROM blogs");
    $cat->execute();
    $row_cat = $cat->rowCount();
    return $row_cat;
}

Then in order to retrieve data on screen, I did this:

$tagSet = new Blogs();
$tags = $tagSet->NumTag();
$tagShow = $tagSet->ShowTag();

if(!empty($tags)){
    $tagShow->GetTags();
}else{
    echo "There is no tag available right now!";
}

But the problem is I get this error message:

Fatal error: Uncaught Error: Call to a member function GetTags() on null in line 20

Which is this line:

$tagShow->GetTags();

So what is the mistake with that ? Can you please help me ?

  • 写回答

2条回答 默认 最新

  • dongpi9480 2018-06-02 06:24
    关注

    The problem with the class function is

    $tagShow = $tagSet->ShowTag(); //returns no value, hence $tagShow is null/empty.
    

    Moreover,

    $tagShow->GetTags();
    

    GetTags() is a function which does not exist in your class. Also, you are calling the function wrong way. $tagShow is originally not an object of your class, it is a variable storing output from ShowTag().

    If you have a function named GetTags(), call it using

    $tagSet->GetTags();
    

    Try using it this way:

    public function ShowTag()
    {
        $blog_tags=array();
        $tag = $this->_db->prepare("SELECT blog_tags FROM blogs");
        $tag->execute();
        while($row = $tag->fetch())
        {
            $blog_tags[] = $row['blog_tags'];
        }
    
        return $blog_tags;
    }
    

    and then in your calling,

    $tagShow = $tagSet->ShowTag();
    
    if(count($tagShow)>0){
        print_r($tagShow);   //Do whatever with the tags array.
    }else{
        echo "There is no tag available right now!";
    }
    

    Try this way and see if it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程