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 } } Link to comment https://forums.phpfreaks.com/topic/282418-call-to-a-member-function-query-on-a-non-object/ 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 } } Link to comment https://forums.phpfreaks.com/topic/282418-call-to-a-member-function-query-on-a-non-object/#findComment-1451060 Share on other sites More sharing options...
mac_gyver Posted September 24, 2013 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. Link to comment https://forums.phpfreaks.com/topic/282418-call-to-a-member-function-query-on-a-non-object/#findComment-1451061 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 Link to comment https://forums.phpfreaks.com/topic/282418-call-to-a-member-function-query-on-a-non-object/#findComment-1451066 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.