-3

how do i solve error message

Warning: mysql_fetch_array() expects parameter 1 to be resource, object given

my code :

if(isset($_POST['login']))
    {
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);

        $res = $MySQLiconn->query(" SELECT * FROM users WHERE username = '$username' ");
        $row = mysql_fetch_array($res);

        if($row['password'] == md5($password))
        {
            $_SESSION['login'] = $row['user_id'];
            echo $_SESSION['login'];
        }
        else
        {
            echo "<script>alert('wrong details')</script>";
        }


    }

Can anyone help ?

amdixon
  • 3,814
  • 8
  • 25
  • 34
Mushaffaq
  • 1
  • 2

2 Answers2

1

The actual problem is you are mixing mysqli methods and mysql methods together.

$res = $MySQLiconn->query(" SELECT * FROM users WHERE username = '$username' ");

is in mysqli and

$row = mysql_fetch_array($res);

is in mysql. so here you are passing $res which is an object mysqli::query returns an object and mysql_fetch_array() expects a resource that is why it shows such an error. . So either use mysql or use mysqli to resolve the problem.

Tintu C Raju
  • 2,700
  • 2
  • 21
  • 25
  • how can you be sure if the OP is using `mysqli`?? Note that `$MySQLiconn` is just a variable here not the actual `mysqli` function – Nishanth Matha Oct 29 '15 at 09:30
  • mysql_query will not return an object `Warning: mysql_fetch_array() expects parameter 1 to be resource, object given` is the error... – Tintu C Raju Oct 29 '15 at 09:32
1

You are mixing mysql and mysqli calls in your code. Use mysqli_fetch_array instead of mysql_fetch_array.

Ninju
  • 2,522
  • 2
  • 15
  • 21
  • how can you be sure if the OP is using `mysqli`?? Note that `$MySQLiconn` is just a variable here not the actual `mysqli` function – Nishanth Matha Oct 29 '15 at 09:31