-2

I created a login page confirmation but when I want to login I find this error:

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in /home/u133082736/public_html/login.php on line 14

This is my code :

<?php
    session_start();
    if($_POST) {
        require_once 'config.php';
        $username = $_POST['username'];
        $password = $_POST['password'];     
        $conn = mysql_connect($dbhost,$dbuser,$dbpass);
            or die ('Error connecting to mysql');
        mysql_select_db($dbname);
        $query = sprintf("SELECT COUNT(id) FROM email_activation WHERE UPPER(username) = UPPER('%s') AND password='%s'");
            mysql_real_escape_string($username),
            mysql_real_escape_string(md5($password)));
        $result = mysql_query($query);
        list($count) = mysql_fetch_row($result);
        if($count == 1) {
            $_SESSION['authenticated'] = true;
            $_SESSION['username'] = $username;
            $query = sprintf("UPDATE email_activation SET last_login = NOW() WHERE UPPER(username) = UPPER('%s') AND password = '%s'",
                mysql_real_escape_string($username),
                mysql_real_escape_string(md5($password)));
            mysql_query($query);
            $query = sprintf("SELECT is_admin FROM email_activation WHERE UPPER(username) = UPPER('%s') AND password='%s'",
                mysql_real_escape_string($username),
                mysql_real_escape_string(md5($password)));
            echo $query
            list($is_admin) = mysql_fetch_row($result);
            if($is_admin == 1) {
                header('Location:admin.php');           
            } else {
                header('Location:home.php');                
            }
        } else {    ?>
<span style='color:red'>Error: that username and password combination does not match any currently within our database.</span>
<?php   }
    }
?>

Please help me understand what caused the error and how to prevent it.

islem
  • 21
  • 4
  • 7
    Possible duplicate of [mysqli\_fetch\_array()/mysqli\_fetch\_assoc()/mysqli\_fetch\_row() expects parameter 1 to be resource or mysqli\_result, boolean given](http://stackoverflow.com/questions/2973202/mysqli-fetch-array-mysqli-fetch-assoc-mysqli-fetch-row-expects-parameter-1) – Qirel Nov 25 '16 at 22:57
  • 1
    Do your own debugging by changing `$result = mysql_query($query);` to `$result = mysql_query($query) or die(mysql_error());`. Your query is failing, which is why you are getting that error message, so you need to figure out what is wrong with your query. – Sean Nov 25 '16 at 23:03
  • The same problem ! :( – islem Nov 26 '16 at 06:44

1 Answers1

0

If your Query returns nothing the parameter is empty and you get the error message mysql_fetch_row() expects parameter 1 , you should debug your query. I would sugguest that you try echo $query and see the result of that, that would be a good starting point.

Link to mysql_fetch_row() documentation

SteffenCH
  • 154
  • 11
  • Okay, please do remember to accept answers if they helped you. – SteffenCH Nov 27 '16 at 20:11
  • Yes ! + can you please see again in my code and try to tell me what's the parse error there ?? i'm a newbie in PHP ! – islem Nov 27 '16 at 20:19
  • I have changed it ! – islem Nov 27 '16 at 20:22
  • Yes ! please see on it and let me know what's the error – islem Nov 27 '16 at 20:26
  • I dont understand what list($count) = mysql_fetch_row($result); is about, Please try `$result = mysqli_query($conn,$query); $row = mysqli_fetch_array( $result, MYSQLI_ASSOC ); ` instead of `$result = mysql_query($query); list($count) = mysql_fetch_row($result); ` – SteffenCH Nov 27 '16 at 20:30
  • i did it , but i got another error (Parse error: syntax error, unexpected T_LOGICAL_OR in /home/u133082736/public_html/login.php on line 8) – islem Nov 27 '16 at 20:38
  • You cant use " or die ()" like that, or is the same as || which is used in if statemets, you need to use and if else statement and not " or die ". – SteffenCH Nov 27 '16 at 20:42
  • Ah Okay , Thank you for your helpful answers , and sorry for my lot questions ! – islem Nov 27 '16 at 20:49
  • No worries were here to help :) – SteffenCH Nov 27 '16 at 20:52