0

I am tring to setup an MYSQLI statement with an Inner Join that worked fine as a mysql_query, but now the SQL won't work.

I'm assuming I'm getting an non-object error because the SQL is not able to return successful. Which I don't understand because all table columns names match perfectly, like I said it works as an old mysql_result, and we have MYSQLI working elsewhere on the page. Could there be corruption to the table itself?

Error:

Warning : mysqli.prepare() [mysqli.prepare]: Couldn't fetch mysqli in file.php on line 123

Fatal Error : Call to a member function execute() on a non-object in file.php on line 123

Code:

<?php
$stmt = $db->prepare("SELECT A.ID, A.HEADER, A.CONTENT, A.PICTURE, A.CATEGORY_ID, C.ID, C.CATEGORY FROM ARTICLES A INNER JOIN ARTICLE_CATEGORY C ON A.CATEGORY_ID=C.ID ORDER BY C.CATEGORY, A.ID DESC");
$stmt->execute();
if ($stmt->bind_result($ID, $HEADER, $CONTENT, $PICTURE, $CATEGORY))
{
  ...
  ...

  while($stmt->fetch()) 
  {
    ...
    ...
    ...
  }
}
?>

Note: Coming from asp.net I'm very noob at mysqli but I'm fairly sure I'm not using the OO method.

I found the answer at this other slightly different case: Problem with mysqli fetch

But this answer required $stmt->store_result() with nested $stmt units, I just needed to clear them so prepare() could have working space.

Turns out the answer is that I was missing $stmt->free_result(); and $stmt->close(); Also for 3 loops on the one page the $stmt variable names need to be kept distinct ex: $stmt1, $stmt2, $stmt3

Community
  • 1
  • 1
  • `$stmt->execute();` => `if(!$stmt->execute()){trigger_error("there was an error....".$db->error, E_USER_WARNING);}` – Funk Forty Niner Mar 08 '16 at 23:13
  • It seems there is more to the error, just above...Warning : mysqli.prepare() [mysqli.prepare]: Couldn't fetch mysqli in file.php on line 123 The $db->error does not process – user3285269 Mar 08 '16 at 23:37
  • 1
    The "couldn't fetch myusqli" error means that `$db` isn't a valid `mysqli` object. I suspect it's a scope problem. – Barmar Mar 08 '16 at 23:48
  • If there are multiple $db objects on one page, do they need distinct names? I have 3 loops on this one page to load content, and one so far using mysqli with no problem. – user3285269 Mar 10 '16 at 01:19
  • Turns out the answer is that I was missing $stmt->free_result(); and $stmt->close(); Also for 3 loops on one page the $stmt variable names need to be kept distinct ex: $stmt1, $stmt2, $stmt3 – user3285269 Mar 11 '16 at 01:44

0 Answers0