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条)

报告相同问题?

悬赏问题

  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。
  • ¥15 GitHubssh虚拟机连接不上
  • ¥15 装完kali之后下载Google输入法 重启电脑后出现以下状况 且退不出去 桌面消失 反复重启没用
  • ¥15 ESP-IDP-BLE配网连接wifi