0

I am trying to run this code to create a simple login form and connect to database my code is

<?php
$avaialableuser=$_POST["username"];
$avaialablepass=$_POST["password"];
$login = mysql_connect("localhost","ahmed","");
if (!$login)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("login module",$login);
$result= mysql_query("SELECT * FROM people"); 
while($row = mysql_fetch_array($result))
  {
  if(($row['username']==$avaialableuser) && ($row['password']==$avaialablepass)){
echo "WELCOME";
}

else
echo "sorry your username or password entered is incorrect please try again";
}
mysql_close($login);
?>
> 

I keep on getting this error [9:03:25 PM] ahmed atwa: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\example\login.php on line 11

can anyone please tell me what am I doing wrong?? Thank you

Marc B
  • 348,685
  • 41
  • 398
  • 480
Aya Abdelsalam
  • 500
  • 3
  • 8
  • 20
  • your query is failing for some reason, which means that $result is not a resource. good practice would be to check if your mysql_query was successful or not. – Julien Nov 28 '11 at 19:18
  • u must write your mysql table – misima Nov 28 '11 at 19:19
  • u can use this code... $result= mysql_query("SELECT * FROM people") or die(mysql_error()); – misima Nov 28 '11 at 19:20

3 Answers3

1

Your connection to the MySQL server was unsuccessful. Since you checked the value of $login, we can assume the the mysql_select_db() statement failed or the query did not work. Please check your database name and try again.

To avoid such problems in the future, check the return value of mysql_select_db() and mysql_query().

George Cummins
  • 27,682
  • 8
  • 67
  • 90
  • Or the query failed for some other reason (i.e. the table doesn't exist) – nickb Nov 28 '11 at 19:21
  • @nickb: Yes, that is possible, and I updated my answer to reflect it. However, the database name ("login module") is likely the problem in this case. – George Cummins Nov 28 '11 at 19:23
1

According to the manual will mysql_query return false if the query fails for some reason.

The two most common reasons are:

  1. The query itself is incorrect
  2. You don't have permission to perform the query.

To narrow it down you should introduce error-checking wherever possible and abort if needed. In your example you have two possible situations were the error can emerge:

  1. mysql_select_db("login module",$login);
  2. $result= mysql_query("SELECT * FROM people");

Both of these two situations return false if failed.

Edit:
In this case the problem is that you're using a space in your database name. To make it work you have to enclose the name using brackets []:

mysql_select_db("[login module]",$login);
Marcus
  • 11,928
  • 5
  • 45
  • 65
1

I don't know if this is true, but I think, you mustn't use Spaces in DB-Names: Couldn't your DB-Name be "login_module"?

mysql_select_db("login module",$login);

€dit: You have to cover your DB-Name in [ brackets ]:

mysql_select_db("[login module]",$login);

ninov
  • 629
  • 1
  • 6
  • 17