0

Possible Duplicate:
mysql_num_rows(): supplied argument is not a valid MySQL result resource

Here's the deal. I wanna make a Login form, but I keep recieving the Error Message:

mysql_num_rows(): supplied argument is not a valid MySQL result resource in Line 14

My code looks like this:

if($_POST){ 
    ob_start();

    $fusuario = $_POST['fusuario']; 
    $fsenha = md5($_POST['fsenha']);

    $sql = "SELECT * FROM usuario WHERE login='$fusuario' and senha='$fsenha'";

    $result=mysql_query($sql);

    $count=mysql_num_rows($result);
    $row=mysql_fetch_array($result);
    if($count==1)
    {
        session_start();
        $_SESSION['admin_user'] = $fusuario;
        $_SESSION['admin_id'] = $row['id_usuario'];
        header("location:index.php");
    }
    else { $erro = 1; }

    ob_end_flush();
} 
?>

When I execute the SELECT query from phpMyAdmin, it returns 1 row, like it should.
When I do it via PHP, no row is returned.

Any ideas?

Community
  • 1
  • 1

2 Answers2

1

It appears that your mysql_query() is running into an error. Check what the error message that mysql is returning:

  $result=mysql_query($sql) or die(mysql_error());
  $count=mysql_num_rows($result);

I'm guessing that there is a problem with your mysql connection... try explicity including the mysql connection identifier:

mysql_query($sql,[INCLUDE LINK IDENTIFIER HERE])

If you're not sure what this means, read this: http://php.net/manual/en/function.mysql-query.php

Ben D
  • 14,321
  • 3
  • 45
  • 59
1

If mysql_num_rows() says that $result is not a valid resource, it's probably because the query failed, and returned FALSE instead of a resource.

You should always check for errors after running an SQL query.
See code examples that check if (!$result) ... at http://php.net/mysql_query


I see from your comment that you had no open connection to the database at the time you issued your query. That'll be a problem too. :-)

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828