views:

276

answers:

3

This is a terrible question because I don't have a simple way to reproduce it. However, I'm using the Zend Framework to connect to my MySQL database on OS X. Sometimes a call to the prepare function on a mysqli object returns null. The stated return values for the prepare function are false or a statement object.

I can't figure out where else to look for info on why the prepare statement is failing. Is there any way to get visibility into the prepare process to see why it is failing? All of my problems are coming up while a transaction is open.

Sorry for the lack of specifics, but I really can't nail down why this is happening.

+1  A: 

Example prepared statement
$result = $db->query( 'INSERT INTO server (key, value) VALUES (:key, :value)', array('key' => $foo, 'value' => $bar)

Can you let us know your DB query?

Try and execute your DB query with test data and see if the query works fine to start with. If the query is ok then we can look why the code fails.

ToughPal
+1  A: 

Just to correct ToughPal, you should be using:

mysqli_query($db, "INSERT INTO table (variable1, variable2) VALUES (hello, mynameis);

Remember that you need to have the db connection defined and stated in the query first, before your actual SQL.

Remember to enclose the table name, column names and value data in backtick escapes.

Shamil
A: 

Well I managed to find the issue over the weekend but was really only able to fix the symptoms and not the cause.

I didn't include any SQL in the original issue because the problem was happening randomly, the same code would sometimes work and sometimes not. The issue looks like it was a memory pointer problem. Whenever I had a problem Zend Debugger told me that I had a mysqli object. I believe this because otherwise I would've gotten an error when trying to run the prepare function on it. I have a singleton object that acts as a container for my mysqli connection but whenever the prepare function failed, === showed that the mysqli being used was not the same as the mysqli connection in my singleton object.

In the end, Zend Framework's only issue is that it doesn't fail if the the prepare function returns null. If you are seeing this problem use === to verify that the connection is actually the same as the one that you've previously initiated.