0

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

When I execute this sql commando:

$sql = "SELECT `id`, `password`, `salt` FROM `users` WHERE `username` = '".mysql_real_escape_string($_POST['name'])."'";

I get this error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /index.php on line 8

That line is my mysql_fetch_assoc for the SQL commando above.

It works when I remove the salt from the SELECT.

But I need the salt selected, so I have tried renamed it in the database and in the SQL commando.

But for some reason that didn't worked aswell.

Then I tried delete the database field, and remake it. But still without luck.

NOTE: the commando runs, but for some reason it still comes up with this error.. I tried to echo out some informations from the selection at the database, and that worked fine. It would actually give me all the selected informations, even the salt, but the error does still show.. Any ideas?

Whole page source:

        if (isset($_POST['login'])) {
            $msg = "Brugernavn og kodeord passer ikke sammen.";
            $sql = "SELECT `id`, `password`, `salt` FROM `users` WHERE `username` = '".mysql_real_escape_string($_POST['name'])."'";
            //echo $sql;
            $query = mysql_query($sql) or die(mysql_error());
            if (mysql_num_rows($query)) {
                while ($row = mysql_fetch_assoc($query)) {
                //print_r($row);
    /*          echo "<pre>";
                print_r($row);
                echo "</pre>";*/
                    $password = md5(hash("sha512",mysql_real_escape_string($_POST['pass']), $row['salt']));
    /*              echo "<pre>";
                    echo $password;
                    echo "</pre>";
                    echo "<pre>";
                    echo $_POST['pass'];
                    echo "</pre>";*/
                    if ($password == $row['password']) {
                        $key = md5( rand(0,1000) );
                        //echo $key;
                        $sql = "UPDATE `users` SET `key` = '".$key."' WHERE `id` = '".$row['id']."'";
                        $query = mysql_query($sql);
                        //login success
                        $_SESSION['key'] = $key;
                        $_SESSION['id'] = $row['id'];
                        if (isset($_POST['remember']) && $_POST['remember'] == 1) {
                            $_COOKIE['key'] = $key;
                            $_COOKIE['id'] = $row['id'];
                        }
                        $msg = "Du er nu blevet logged ind.";
                    }
                }
            }
        }
Community
  • 1
  • 1
  • 1
    please be aware that the `mysql_xxx()` functions are deprecated and not recommended for use. It is strongly recommended to switch to use either the `mysqli_xx()` functions or the PDO library. See [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for more info. – SDC Dec 19 '12 at 17:21
  • Try to execute the query in phpMyAdmin, changing the value for username. The query will not succeed, so debug it. – Richi González Dec 19 '12 at 17:39

0 Answers0