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

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
sydneyguyAsked:
Who is Participating?
 
edreanCommented:
Where is $osDB defined?
0
 
jrm213jrm213Commented:
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
 
sydneyguyAuthor Commented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
sydneyguyAuthor Commented:
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
 
jrm213jrm213Commented:
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
 
sydneyguyAuthor Commented:
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
 
jrm213jrm213Commented:

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

Open in new window

0
 
edreanCommented:
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
 
sydneyguyAuthor Commented:
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
 
sydneyguyAuthor Commented:
on the doller thanks for your help
garry
0
 
Ray PaseurCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.