0

For some reason, some of my SQL queries seem to be handling numbers oddly. For instance, in my mySQL database I have a table called users with an INT(11) field called points. When I try to retrieve this field, sometimes it returns correctly, and other times it returns null. This query returns "User fMcBBx has points according to database" every time:

$query = "SELECT points FROM users WHERE user_id = '$this->getID()'";
    $result = DBCore::call()->fetchRow($query);
    echo "User {$this->getID()} has {$result['points']} points according to database";

While this one from my constructor returns the number correctly every time, such as "User fMcBBx has 1050 points according to database":

SELECT first,
       last,
       email,
       points,
       level,
       gender
FROM users
WHERE user_id = '{$this->id}'

I haven't found any patterns in the queries that do and don't return this field correctly. I've checked all of them by running them against the database in PHPmyAdmin to make sure they all worked, and I've echoed all of the input values to make sure they are being transferred correctly. Why might I get nulls on some queries but not others?

jaimerump
  • 882
  • 4
  • 17
  • 35
  • It'd be good if you can provide some test data and queries that reproduce this behavior. Just looking at your question, I don't see why those queries would produce different points results. – dcp Jul 03 '12 at 17:27
  • Is it the same queries that always return nulls, or does the same query return different things when it's run at different times? It's not entirely clear from your question, but I suspect it's the former. And to echo @dcp - it would help if we could see some of the queries that fail, as well as the structure of tables and some sample data. – andrewsi Jul 03 '12 at 17:36
  • what makes you think it returns null? Why wouldn't it just not return any row? Why do you think it's the query? Why do you think it can't be your this.getID() parameter that could be different at both different times? – Sebas Jul 03 '12 at 17:40
  • Try with concrete id instead of $this->getID() and $this->id – kingdaemon Jul 03 '12 at 17:45

1 Answers1

0

When I put braces around the function call in the first query, $query = "SELECT points FROM users WHERE user_id = '{$this->getID()}'";, it works perfectly. Php can't evaluate function calls inside double-quoted strings the way it can read variables, putting curly braces around the function call allows it to evaluate the function call correctly.

jaimerump
  • 882
  • 4
  • 17
  • 35