travisco87 Posted September 24, 2013 Share Posted September 24, 2013 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 } } Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted September 24, 2013 Share Posted September 24, 2013 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 } } Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted September 24, 2013 Solution Share Posted September 24, 2013 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. Quote Link to comment Share on other sites More sharing options...
travisco87 Posted September 24, 2013 Author Share Posted September 24, 2013 Ahhh! I was using it in earlier code to grab some information but then wrote this $DBH = null; Which was why it was not doing what I expected it to do, Thanks for the help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.