user2938704
user2938704

Reputation:

Searching an array for username

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

Upvotes: 0

Views: 85

Answers (3)

Digital Chris
Digital Chris

Reputation: 6202

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.

Upvotes: 1

stackrocha
stackrocha

Reputation: 405

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)

Upvotes: 1

blue
blue

Reputation: 1949

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)."...."

Upvotes: 0

Related Questions