0

Im trying to return a record from my table with the following class...

class User {
   public function getCredits($uid)
   {
       $rs = mysql_query("select bank from `users` where id = '$uid'");
       $row=@mysql_fetch_object($rs);
       return $row->bank;
   }
}

I output this data with the following...

    <?php print $User->getCredits(); ?>

Im not given anything however, as though my code cannot be read and my broweser just display a white page?

4
  • 2
    Don't put @ in your code. It's hiding any errors that might occur. My guess is mysql_fetch_object is throwing an error and you're suppressing it. Commented May 21, 2013 at 20:50
  • 1
    and please do not use an mysql_* functions. Instead go with mysqli_ or PDO! Commented May 21, 2013 at 20:51
  • 1
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. People who want this message: gist.github.com/MadaraUchiha/3881905 Commented May 21, 2013 at 20:52
  • I never seems to amaze me how many of this questions would not have been asked if the poster actually did any kind of rudimentary debugging for themselves before asking the question here. A simple var_dump() of $uid, the actual query string, or of $row would have revealed this problem immediately. The first thing you should always do when code is not behaving the way you expect it to is to debug. You should of course also have code to handle all possible edge cases (query failure, query returning 0 rows, etc.). Commented May 21, 2013 at 21:04

3 Answers 3

3

You have:

   $rs = mysql_query("select bank from `users` where id = '$uid'");

but call the method with ->getCredits(), so $uid is going to be a null, and your query ends up being

 select bank from users where id = ''

Assuming the id field is a standard integer/auto_increment, there will be NO records that have a blank id, so you get back an empty set. So fetch_object will return a boolean false to indicate a fetch failure (no records to fetch), and you blindly try to use that boolean false as your data elsewhere.

Sign up to request clarification or add additional context in comments.

Comments

2

You are not passing the function a parameter:

<?php print $User->getCredits(); ?>

Pass it the proper $uid for the query. Also, see @BenjaminGruenbaum comment on deprecated extension.

Comments

0

Try this:

class User {
   public function getCredits($uid)
   {
       $rs = mysql_query("select bank from `users` where id = '".$uid."'");
       $row= @mysql_fetch_array($rs);
       return $row['bank'];
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.