Solved

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

Posted on 2010-09-13
11
1,132 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
ID: 33663007
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
ID: 33663027
Where is $osDB defined?
0
 

Author Comment

by:sydneyguy
ID: 33663071
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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:sydneyguy
ID: 33663084
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
ID: 33663143
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
 

Author Comment

by:sydneyguy
ID: 33663161
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
ID: 33663188

{
      /* 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
ID: 33663200
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
ID: 33663272
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
ID: 33663282
on the doller thanks for your help
garry
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33663313
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

838 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