Function echoing causes error - php

I have made a public function for getting, and showing a user's infraction info. When I put it on a page, it only shows what I have in the function, and not any of the other content on the page. It only shows the table headers, and none of the data. I get this error:
Fatal error: Call to a member function query() on a non-object in /Applications/AMPPS/www/classes/user.php on line 108
Also, I have other functions from the same class that work fine.
Here is the code for the function link (sorry about pastebining it, it was really long)

Your $db object is null or can't be accessed. Your line 108 error does not match up with your code you have pasted and you don't have the code where you are creating your database object to see what may be wrong there.

The error message seems to indicate that your "$db" is not set to an object. Make sure its initialized properly.

The function never initializes the $db variable. If it's a class property, it should be $this->db or self::$db. If it's a global variable, you need to put global $db; at the beginning of the function.

Related

Can't access a global variable in PHP-"Fatal error: Call to a member function query() on a non-object"

There are a number of Q's on this topic but I wasn't able to get it to work…thanks in advance for any and all help you can give.
Basically, I'm trying to do an AJAX call and it is working successfully. The call is made to a file named stores.php. Within that file, the following function is called:
function function1($ABC){
echo "…";
...
$var = function2($DEF);
… //do stuff with var
}
The trouble crops up in function2:
function function2($GHI){
global $db;
$query ='SELECT …';
… // more...
$results = $db->query($query);
return $results;
}
For the query() call to $db, I get the error: "Fatal error: Call to a member function query() on a non-object…"
I don't really understand this because I am inviting it into the function with 'global'. In some of the other Q's, people discussed some sort of scoping problem when a function is called from within another function that I didn't really understand, but I thought the point was that the function gets its predecessors scope. So I went around declaring it global wherever I could think of that would be relevant, and it didn't work.
I even re-included the script that originally created the $db variable in that function, and it still felt it was null!
NOTE: PLease keep in mind that the file, stores.php contains many functions that all successfully use "global $db" in making queries of the db.
Thanks again...
Ok, the problem was very simple, but also very specific to my page. Basically, stores.php (the file which contained the functions) was being included [via include()] to a main page. Earlier in that page, the variable $db was being set. Therefore, all the functions that were written in the stores.php page were able to access $db once it was declared with 'global'.
But when I was making the call that was failing, I was calling the function from stores.php directly--not the main page. And on that page, $db isn't ever created at all! So it was null.
SInce you are using a variable which is global, make sure that none of your functions that use global $db, will not overwrite it. Best way for making sure, if the global $db actually is object, is to use print_r($db) inside the function where that exception gets thrown.

Class method chaining

I have the following code but it does not seem to want to work chained.
$this->view->setData($class_vars);
$this->view->render('addview');
The above works and runs fine but when i try to do the following:
$this->view->setData($class_vars)->render('addview');
I get the following error:
Fatal error: Call to a member function render() on a non-object in....
But the strange thing is when i call it the other way:
$this->view->render('addview')->setData($class_vars);
It runs, but I need the setData to run first as this sets up the var for the actual view, so even though i get the view its got errors where the vars should be? Both methods are public?
Thanks You
Does setData() return the view object (i.e. it has return $this; line)? If not... well it should if you want it to work this way.
For further reference. This technique is called 'fluent interface' and is described here:
http://www.martinfowler.com/bliki/FluentInterface.html

Call to member function on a non-object

I've read several of the threads that already exist with this, or a similar, name. None seems to address my situation exactly.
I'm getting this error from my utils.php class.
Notice: Undefined variable: debug in /app/www/utils.php on line 89 Fatal error: Call to a member function debug() on a non-object in /app/www/utils.php on line 89
I define the variable debug like this:
require_once('PHPDebug.php');
$debug = new PHPDebug();
and then call it (in line 89) like this:
$debug->debug($message);
The reason I'm so baffled is that I copied and pasted these lines from my index.php and that call works just fine.
If you want, I can include links to the index.php and utils.php files, as well as PHPDebug.php.
Thanks to your last comment, the solution to your problem is to declare $debug as global within the function. Thus, you should have something like this:
require_once('PHPDebug.php');
$debug = new PHPDebug();
function myfunction()
{
global $debug;
// some code
$debug->debug($message);
}
You can read more about global in the official doc.

pdo error: Product sub variant: Product: ( Fatal error: Call to a member function prepare() on a non-object

I am using pdo in php and the error i am getting is:
Product sub variant: Product: (
Fatal error: Call to a member function prepare() on a non-object
i have this piece of code in a class and i call it in php, the class is included in the page im calling it.
function get_variantname($variant_id)
{
$query = $db->prepare("select parent_attribute_label from tbl_parent_attribute where parent_attribute_id = :variant_id");
$query->bindParam(":variant_id", $variant_id);
$query->execute();
while($row = $query->fetch(PDO::FETCH_OBJ))
{
return $row->parent_attribute_label;
}
}
You are trying to incorrectly access the $db variable, which looks like a global. Add
global $db;
at the beginning of your function.
The error message tells you that prepare is being called on something that is not an object. But we all know it is an object, since you set it once to some value (there's no chance that it might be e.g. a string). That leaves only the possibility of it not having the value you set inside the function, for some reason. Which leads us to the answer.
You have not passed $db into the function, so it doesn't know what $db->prepare() is supposed to do. If you wish to have global access to your $db class I'd suggest a singleton class. You can also declare $db as a global variable.

Fatal error: Call to undefined method stdClass

I get an error that says
Fatal error: Call to undefined method stdClass::mysql_con() in ........../.../includes/script/import.php on line 68.
Line 68 corresponds to:
if(!$ip2c->mysql_con())
I do have a require_once() statement at the beginning of my script
What could be the problem here?
Thanks
Dusoft says it could mean:
$ip2c object does not exist,
Which is not correct because you would get a different error "Fatal error: Call to a member function mysql_con() on a non-object"
He also says it could mean:
mysql_con function is not part of the class you are trying to call
Which is true but not so helpful cos its very difficult to add methods to stdClass.
Additionally it could be to do with serialisation quote:
This error is normally thrown when a class instance has been serialised to disk, then re-read/deserialised in another request but the class definition has not been loaded yet, so PHP creates it as an "stdClass" (standard class.)
Or most likely, I think:
the $ip2c variable was not an object and then php silently cast it to become stdClass somewhere in the code above.
This could happen if you directly assign a property on it.
Like:
$ip2c = null;
//php casts $ip2c to 'stdClass'
$ip2c->foo = bah;
//Fatal error: Call to undefined method stdClass::mysql_con() in...
$ip2c->mysql_con();
See a better example here.
it means that either $ip2c object does not exist or mysql_con function is not part of the class you are trying to call.
I think this happen because "extension=php_mysql.dll" extension isn't loaded in php.ini.
Take a look with
phpinfo();
It could be incorrect code. I once managed to get that error when I had this line of code:
if ($myObj->property_exists('min')){
// do something
}
Which resulted in error line like this:
PHP Fatal error: Call to undefined method stdClass::property_exists() in myFile.php on line ###
I later fixed the line to:
if (property_exists($myObj, 'min')) {
// do something
}
So check for that possibility as well.
Most likely the object does not exist. Please show us the code of how you created it. If you are using it within another class (maybe creating it in the __construct function for example), using:
$ip2c = new Class;
Won't cut it. Instead do:
$this->ip2c = new Class;
and then
$this->ip2c->mysql_con();

Categories