On line 5 you are assigning the same value to $result over and over again. ... so ... condition is always true (it is always first row of the table). I would remove line 5. The $result on line 9n is also causing trouble. What is the goal here? Maybe you post the database structure.