Jump to content

Call to a member function query() on a non-object


travisco87

Recommended Posts

I am writing a function to grab and count how many times a certain value is used in a table but get this error,

Fatal error: Call to a member function query() on a non-object in C:\wamp\www\Rachels\includes.php on line 61

I have tried looking up the error and found that it sometimes occurs when a variable is out of scope. I am passing my connection through the scope so I do not think that is the issue. My code is as follows 

function getTagCount($inTagId=null, $inTagName=null, $DBH) {
    
    //Make the connection and grab all the tag's TAG TABLE HAS TWO FIELDS id and name
            $stmt = $DBH->query("SELECT * FROM tags");
            $stmt->execute();
            
            //For each row pulled do the following
            foreach ($stmt->fetchAll() as $row){
                //set the tagId and tagName to the id and name fields from the tags table
                $tagId = $row['id'];
                $tagName = $row['name'];
                
                //Next grab the list of used tags BLOG_POST_TAGS TABLE HAS TWO FILEDS blog_post_id and tag_id
                $stmt2 = $DBH->query("SELECT * FROM blog_post_tags");
                $stmt2->execute();
                $blogTagList = array();
                $blogTagList = $stmt2->fetchAll();
                $tagCount = array_count_values($blogTagList);
                //Print the following list 
                echo '<li><a href="popular_tags.php?=' . $tagId . '" title="' . $tagName . '">' . $tagName . '(' . $tagCount[$tagId] . ')</a></li>';
            //End of loop - start again
            }
            
}

It means you're either not initializing your $DBH or it's not actually an object.

 

I am writing a function to grab and count how many times a certain value is used in a table but get this error,

Fatal error: Call to a member function query() on a non-object in C:\wamp\www\Rachels\includes.php on line 61

I have tried looking up the error and found that it sometimes occurs when a variable is out of scope. I am passing my connection through the scope so I do not think that is the issue. My code is as follows 

function getTagCount($inTagId=null, $inTagName=null, $DBH) {
    
    //Make the connection and grab all the tag's TAG TABLE HAS TWO FIELDS id and name
            $stmt = $DBH->query("SELECT * FROM tags");
            $stmt->execute();
            
            //For each row pulled do the following
            foreach ($stmt->fetchAll() as $row){
                //set the tagId and tagName to the id and name fields from the tags table
                $tagId = $row['id'];
                $tagName = $row['name'];
                
                //Next grab the list of used tags BLOG_POST_TAGS TABLE HAS TWO FILEDS blog_post_id and tag_id
                $stmt2 = $DBH->query("SELECT * FROM blog_post_tags");
                $stmt2->execute();
                $blogTagList = array();
                $blogTagList = $stmt2->fetchAll();
                $tagCount = array_count_values($blogTagList);
                //Print the following list 
                echo '<li><a href="popular_tags.php?=' . $tagId . '" title="' . $tagName . '">' . $tagName . '(' . $tagCount[$tagId] . ')</a></li>';
            //End of loop - start again
            }
            
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.