views:

26

answers:

2

Hello there, I have PHP 5.2.3, apache2.x, mysql 5.x on a Linux machine. EVerything was working fine untill yesterday. This morning all of a sudden one of the php files started to throw "Fatal error: Call to a member function execute() on a non-object in". I use PDO (prepare, execute, close cursor etc). Have you ever come across this problem? does someone know a fix for this please.

Many thanks, R

+2  A: 

Your prepare call is probably failing. This is probably due to a SQL statement that is not valid.

We would need to know exactly what library you are using to know more, but prepare is probably returning false instead of an object. Then when you try to call execute on the return value, you are trying to call it on false instead of an object, causing the error message you see.

If you echo or var_dump the value you are calling execute on, you will be able to see more details.

Alan Geleynse
Invalid SQL in a prepared statement produces a "SQLSTATE[42000]: Syntax error or access violation" error, rather than this error. Pretty much anything can be prepared successfully (I just tried "asl aslkjd" and it returned a PDO statement object).
Alex JL
That depends on the library being used. That SQL error could be translated by the SQL access library or driver into a null or false return value, which would cause this error.
Alan Geleynse
Right, I just saw that in the PDO manual (about some drivers using emulated prepared statements, and not checking the syntax). I'm using the same set up as racky, though (php 5.2.3, Mysql 5.1) - I wonder why it would be different?
Alex JL
Thank you folks, I seem to have an error in my sql statement after I added the line suggested by "lonesomeday" above. Let me get over it and see if that helps :)
racky
A: 

As Alan says, the problem is probably that prepare is returning false rather than a PDOStatement. The best way to debug this is to turn warnings on. To do this, put this code right after you initialise PDO:

$db = new PDO();  // insert the following after this line
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );

This will mean that a PHP warning will be raised by PDO when the prepare statement fails. This should contain information that will help you debug the problem.

lonesomeday
Thank you LSD, I seem to have an error in my sql statement after I added the line above. Let me get over it and see if that helps :) Thanks again!
racky
Thanks, that fixed it :)
racky