Search code examples
phpmysqlzend-db

MySQL or Zend sometimes returning nulls instead of numbers


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?


Solution

  • 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.