Solved

php function call Fatal error: Call to a member function getAll() on a non-object in

Posted on 2010-09-13
11
1,056 Views
Last Modified: 2012-05-10
have a php site doing some sql extraction, and what i am doing is calling a function,
now the original code is as below when i call the code the translator.php loads and runs the code outside the function and works okay then i try to call function on the translator page from the calling page by $findheadings = findheadings(); and it  throws up the Fatal error: Call to a member function getAll() on a non-object in.
little help to get me started would be appreciated
...... calling page  code to call page
<?php
include( 'translator.php' );  /* this works */
$findheadings = findheadings(); /* this causes the error */
?>
..... translator.php
<?php
function findheadings()
{
      /* SELECT * FROM translator */

      $questionusers = $osDB->getAll("select * from ! where searchline like '%Muscles%'",array('translator') ); /* this crashes */
}
      
$questionusers = $osDB->getAll("select * from ! where searchline like '%Muscles%'",array(translator') );  /* this runs */

?>
......end translator page
0
Comment
Question by:sydneyguy
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 17

Expert Comment

by:jrm213jrm213
Comment Utility
I think in you might need to change your function to read $osDB as a global variable so it can access it
function findheadings()
{
      /* SELECT * FROM translator */
      global $osDB;
      $questionusers = $osDB->getAll("select * from ! where searchline like '%Muscles%'",array('translator') ); 
}

Open in new window

0
 
LVL 3

Accepted Solution

by:
edrean earned 250 total points
Comment Utility
Where is $osDB defined?
0
 

Author Comment

by:sydneyguy
Comment Utility
the global worked but i cannot see it defined on the calling page were i grabed the code from.
all i  did was to place the translator page into the dir and then palced the same code that works on the calling page onto the translator page then caled it via a function,
but i cannot see were this global $osDB; is defined any were on the other page??
0
 

Author Comment

by:sydneyguy
Comment Utility
question i am guessing that its not good to use global $osDB; in the system is it not better to pass the $osDB; to the called page
0
 
LVL 17

Expert Comment

by:jrm213jrm213
Comment Utility
It is probably in an included file somewhere.
As far as passing it to the page or declaring it global, that is up to you. By passing it you *should* always know that it exists and is open, whereas calling it via global you are assuming it exists as what you expect and that it is open. I figured you had created and were using that object...
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:sydneyguy
Comment Utility
ok then this works but how do you return the array $findheadings  back to the calling page, when it returns its just blank?
function findheadings()
{
      /* SELECT * FROM translator */
        global $osDB;
        $findheadings = $osDB->getAll("select * from ! where searchline like '%Muscles%'",array('osdate_translator') );

}
0
 
LVL 17

Assisted Solution

by:jrm213jrm213
jrm213jrm213 earned 250 total points
Comment Utility

{
      /* SELECT * FROM translator */
        global $osDB;
        $findheadings = $osDB->getAll("select * from ! where searchline like '%Muscles%'",array('osdate_translator') );
 return $findheadings;
}

Open in new window

0
 
LVL 3

Expert Comment

by:edrean
Comment Utility
The risk with declaring a variable as global is that it could be altered anywhere in the code and cause unexpected results. It depends on the code though...
0
 

Author Comment

by:sydneyguy
Comment Utility
thanks for that i keep forgetting that return $findheadings; in my other languages this is a no no so that works fine thanks for that
as for the global DB i will run with this and let my lknowledge grow to recode the question
thanks for both of your time and support
garry
0
 

Author Closing Comment

by:sydneyguy
Comment Utility
on the doller thanks for your help
garry
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Call to a member function getAll() on a non-object pretty much says it all.  An object was not yet instantiated.  This looks like a variable scope issue, since you can get past it with a global declaration.  But as a coding practice it would make more sense to place the object in $GLOBALS rather than inject it into the global scope.

Or you might be able to pass the object in the function argument:
function findheadings($osDB)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now