0

This code is looping through an array of what is in the database, that searches the username variable against the Usernames in the database, when its found one it should break from the if else statement.

<?php 
while($row = mysql_fetch_array($resultSet, MYSQL_ASSOC))
    {
if($username = $row['User_Name']){
     echo "username found";
     echo "Logged in as ", $Username;   
     break;
    }
else
    {
    echo "not yet";
    }
}
    mysql_close($conn);
    ?>
</div>

I am encoutering this error:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in H:\STUDENT\S0190204\GGJ\Logon-process.php on line 36
  • 1
    and what is the problem? – MSadura Apr 01 '14 at 14:00
  • $row['User_Name'] - is this correctly? WRONG: echo "Logged in as ", $Username; Correct: echo "Logged in as ", $username; // lowercase U Wrong: if($username = $row['User_Name']) Correct: if($username == $row['User_Name']) – Tyralcori Apr 01 '14 at 14:01
  • I don't see any question. – va5ja Apr 01 '14 at 14:01
  • 1
    You should definitely make that check in the sql... – blue Apr 01 '14 at 14:01
  • if($username == $row['User_Name']) (u used single =) – MSadura Apr 01 '14 at 14:01
  • 1
    Error tells you the problem. You need to check how you get $resultSet filled. – va5ja Apr 01 '14 at 14:03
  • Smth. is wrong with your sql query. Seems that the $resultSet is false – lvil Apr 01 '14 at 14:04
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Apr 08 '14 at 02:55

3 Answers3

1

Where you have

if($username = $row['User_Name']){

it should be

if($username == $row['User_Name']){

single equals sets, double equals tests equality, triple equals tests literal equality.

Really though, this is not the way to do it, you should include

 WHERE user_name = "$username"

in the query rather than returning all results and looping.

Also, don't use mysql_* functions, they're deprecated and don't include built-in secure practices like passed parameters.

Digital Chris
  • 6,177
  • 1
  • 20
  • 29
1

this

if($username = $row['User_Name']){

should be

if ($username == $row['User_Name']){

however, there's many other ways to do this.. (like the answer above for example)

stackrocha
  • 405
  • 2
  • 8
0

You should avoid passing such a big data and searching in a loop - there is SQL for that.

Your query should look smth like:

"SELECT .... WHERE `User_Name` = ".mysql_real_escape_string($username)."...."
blue
  • 1,939
  • 1
  • 11
  • 8