0

I know this question has been asked many times before. But each one seems to always have pointed to a failed MySQL query and suggests to check the mysql_query() results.

However I do check the results, and the query returns a resource with two rows. Additionally I ran the query from my command line and it also returns two rows. What am I missing.

$query = "SELECT * FROM sleep WHERE date='2016-08-21'";

$result = mysql_query($query);

    if($result) {
        die("Database query failed");
    } else {
        echo "success";
    }

    if( mysql_num_rows($result) == FALSE) {
        $mode = "print";
    } else {
        $mode = "edit";
    }
    echo $mode;
Hispanic 100
  • 119
  • 1
  • 7
  • `if($result)` is **BACKWARDS**. mysql-query returns false on failure, which you claim is "success". Since your query did fail, and your test for failure failed, you then propagated that failure onwards. – Marc B Aug 26 '16 at 20:23
  • **WARNING**: If you're just learning PHP, please, do not use the [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) explains best practices. Make sure your parameters are [properly escaped](http://bobby-tables.com/php) and there are [SQL injection bugs](http://bobby-tables.com/) or they can be exploited. – tadman Aug 26 '16 at 20:51
  • Also you should probably learn more about what PHP considers logically true if you have things like `if (x == FALSE)` in your code. You're setting up a double negative there: The edit condition evaluates as "if number of rows is not non-false". – tadman Aug 26 '16 at 20:53

0 Answers0