?
Solved

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

Posted on 2010-09-13
11
Medium Priority
?
1,170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 

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 1000 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 111

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 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