0

My code is this:

<?php
    $user=$_SESSION['username'];
    $avatar="SELECT us_avatar FROM all_users WHERE user_nick=?";
    $query=$bd->prepare($avatar);
    $query->bind_param('s',$user);
    $query->execute();
    $query->bind_result($img);
    $query->fetch();
echo '<img src="'.$img.'" alt="" />';
?>

The error is - Fatal error: Call to a member function bind_param() on a non-object in i've checked does avatar is reserved keyword but it isn't. I've tried "SELECT us_avatar FROM all_users WHERE user_nick=?" but it doesn't work. Name of the field and the table are the same as in the db. That bug is coming after update, if i don't do the update it run well.

  • `$query` is not an object. But you're treating it as one. That gives you the fatal error (and is what the error message tells you). Please see [Reference - What does this error mean in PHP?](http://stackoverflow.com/q/12769982/367456) - Recommended reading: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) – hakre Apr 20 '14 at 11:48
  • I include the $bd in the top of the file. All with the connection is ok. If it isn't i will can't logon. –  Apr 20 '14 at 11:49
  • @user3496946, Well how we do we know that unless you tell those things ? Also, we don't find a `session_start();` – Shankar Narayana Damodaran Apr 20 '14 at 11:52
  • Take note that `PDO::prepare()` can return `FALSE` in case of an error (and depending on the type of PDO error-handling that you're using), therefore you must not get a `PDOStatement` as return type (see: http://www.php.net/manual/en/pdo.prepare.php). But you do not handle that error-case in your code. Hence you see the error. You need to handle the error case and let PDO tell you what the error was (using PDO exceptions instead of returning FALSE might be more handy in your case). See [PDO error handling](http://stackoverflow.com/q/16414287/367456) and http://php.net/pdo.error-handling . – hakre Apr 20 '14 at 11:52

1 Answers1

0

use this way:

<?php
  $user=$_SESSION['username'];
  $query = $bd->prepare('SELECT us_avatar FROM all_users WHERE user_nick=?');
  $query->bind_param('s', $user);
  $query->execute();
  $query->bind_result($img);
  $query->fetch();
  echo "<img src='{$img}' alt='' />";
?>
train_fox
  • 1,517
  • 1
  • 12
  • 31
  • And how should that prevent the fatal error of non-object `$query` if I may ask? – hakre Apr 20 '14 at 11:51
  • bind_param works on prepare function not in it result. – train_fox Apr 20 '14 at 11:53
  • Sorry. There is one another possibility. If you have sql syntax error then this error happened. Check your select syntax. – train_fox Apr 20 '14 at 11:54
  • I think that the select syntax is ok, and some aditional info at first when i load the page all is ok or when i reload the page, but when i do an mysqli update that error shows. –  Apr 20 '14 at 11:56
  • Then why you show us select syntax? What about your update syntax? – train_fox Apr 20 '14 at 12:00
  • It is working ok with no errors, the problem is after the update with the select not with the update –  Apr 20 '14 at 12:03