-1

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

Hello im trying to get my login system going and i keep getting this error here is my code:

<?php
$host="localhost"; 
$username="root"; 
$password="power1"; 
$db_name="members"; 
$tbl_name="users"; 

$link  = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$Email=$_POST['Email'];
$Password=$_POST['Password'];

$Email = stripslashes($Email);
$Password = stripslashes($Password);
$Email = mysql_real_escape_string($Email);
$Password = mysql_real_escape_string($Password);


$sql="SELECT * FROM $tbl_name WHERE Email='$Email' AND password ='$Password'";
$result=mysql_query($sql, $link) or die ('Unable to run query:'.mysql_error());


$count=mysql_num_rows($result);


if($count==1){
session_register("Email");
session_register("Password");
header("location:login_success.php");
}
else {
echo "Wrong Email or Password";
}
?>

Its working now

Community
  • 1
  • 1
spencer
  • 39
  • 3
  • 4
  • 9
  • what does `echo $sql` prints? – k102 Jul 04 '11 at 05:40
  • Parse error: syntax error, unexpected T_IF, expecting ',' or ';' in C:\xampp\htdocs\html\Login\checklogin.php on line 26 – spencer Jul 04 '11 at 05:42
  • 1
    http://stackoverflow.com/questions/5473981/warning-mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-i –  Jul 10 '12 at 11:54

3 Answers3

2

It can be that your query is failing for some reason, try

$link  = mysql_connect("$host", "$username", "$password")or die("cannot connect");

//....

$sql="SELECT * FROM $tbl_name WHERE username='$Email' AND password ='$Password'";
$result=mysql_query($sql, $link) or die ('Unable to run query:'.mysql_error());

or

if(!$result) die ('Unable to run query:'.mysql_error());

When query fails, it returns FALSE, hence the BOOLEAN you are passing to mysql_num_rows(); You should always check if a result actually exists before going on with your code.

Damien Pirsy
  • 25,003
  • 8
  • 68
  • 77
1

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

http://php.net/manual/en/function.mysql-query.php

You have an error in your query.

jessica
  • 2,953
  • 2
  • 28
  • 30
1

Try this one:

$sql="SELECT * FROM ".$tbl_name." WHERE username='".$Email."' AND password ='".$Password."'";
Anthony
  • 11,737
  • 9
  • 66
  • 101
Marco
  • 802
  • 6
  • 17
  • 39