1

I've been trying for a few days now to fix this part of my PHP code, but nothing seems to be working.

I'm basically a super-newb. Pretty much last week I decided to try and make a blog from scratch using PHP. This requires that I learn PHP. So, by following tutorials and reading sites like w3schools, I've slowly been learning applicable PHP.

Anyway, here is the part of my code I'm working on:

$query = $db->prepare("SELECT post_id, title, body, date_posted, category
                       FROM posts INNER JOIN categories ON
                       categories.category_id=posts.category_id ORDER BY
                       post_id desc limit $start, $per_page");
$query->execute();
$query->bind_result($post_id, $title, $body, $date_posted, $category);

$author = $db->prepare("SELECT username FROM posts INNER JOIN user ON
                            user.user_id=posts.user_id");
print_r( $author->error );
$author->execute();
$author->bind_result($username);

Let me explain. I have 3 database tables: posts, categories, and user. Most post information is in "posts," including category_id and user_id, but not the actual names of the category and user. I get those from the other two tables. $db uses mysqli.

The $query part of the above code works perfectly fine. The $author prepare line is where I'm having an issue. I wrote that line based on the $query prepare line that works.

But when I run the code, I get "Notice: Trying to get property of non-object." This then leads to $author->execute() not working and producing the error: "Fatal error: Call to a member function execute() on a non-object."

So again, since I'm a mega-newb, I assume the fix is something really simple that probably has to do with my ignorance of PHP. Does anyone have any idea or suggestions? Thanks in advance for the help.

jeroen
  • 91,079
  • 21
  • 114
  • 132
Homer
  • 45
  • 1
  • 1
  • 3
  • duplicate of http://stackoverflow.com/a/15447204/285587 – Your Common Sense Mar 17 '13 at 00:58
  • Using your suggestion of adding "or trigger_error($db->error)", I get the error message: Notice: Commands out of sync; you can't run this command now. I'm researching what this means right now. Looks like you just can't have two queries back to back? – Homer Mar 17 '13 at 01:13

1 Answers1

5

The error is saying that $author is a non-object.

If $author = $db->prepare() had executed properly it would have returned a mysqli_stmt object. Instead it probably returned false

You can this confirm by:

echo($author); // Should be false
echo($db->error); // A string representation of your error.

Edit for posterity:

A better practice, fault tolerant implementation of a prepare() statement might be something like below, as is described here:

$stmt = $mysqli->prepare($query) or trigger_error($mysqli->error);

You should check out the docs here: http://www.php.net/manual/en/mysqli.prepare.php

Also, if you are new to PHP, I would recommend the PDO libraries instead of mysqli, they are a little easier to work with once you start fetching results.

Community
  • 1
  • 1
Evan Cordeiro
  • 773
  • 10
  • 12
  • The goal here was to describe what was happening to @Homer. Not to write a book on coding best practice. In fact, the PHP documentation that I referred to in the link simply wraps the prepare statement in a conditional and returns on false. If you downvote every simplified explanation for a beginning looking to learn you are going to be an extremely busy person. – Evan Cordeiro Mar 17 '13 at 01:07
  • Thank you Evan and Common Sense for the help. Either of your error methods show the same key statement: "Notice: Commands out of sync; you can't run this command now." Knowing that, I was able to find that after my "bind_result" line I just need to add "$query->store_result();" Now my code works. However, I don't understand what this line does or means, and how it relates to the previous "non-object" errors. Any explanation or links would be greatly appreciated. Thanks again. – Homer Mar 17 '13 at 01:29