Jump to content

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


Go to solution Solved by mac_gyver,

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
            }
            
}
  • Solution

your need to debug what your code is doing to find out what is in $DBH and then to find out why it isn't what you expect.

 

what does var_dump($DBH); show? backtrack from there to the point here you are initializing $DBH.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.