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