Jump to content

while loop problem


canabatz

Recommended Posts

$dacode="select * from `user_register` where username='user1' and password='user1'";
$result=mysql_query($dacode) or die('Error, query failed');

while ($row=mysql_fetch_assoc($result))
{
       if( $row['city'] == $_POST['city']) 

         {
echo "found it";

         }

     else { 
       echo "didnt find it":
      }


}

 

user1 have few citys in is list ,so when im sending a form to check if one of the citys are in the list

it's just checking first row ,like the loop is not working!

 

what im dong wrong?

 

thanx

Link to comment
https://forums.phpfreaks.com/topic/186022-while-loop-problem/
Share on other sites

i have this as example for table

 

usename  password  city

 

user1    user1      city1

user1    user1      city2

user1    user1      city3

user1    user1      city4

user1    user1      city5

user1    user1      city6

user1    user1      city7

user1    user1      city8

 

 

i want in my code to check if city6 for example is allready in the list!

thanx

Link to comment
https://forums.phpfreaks.com/topic/186022-while-loop-problem/#findComment-982323
Share on other sites

$dacode="select * from `user_register` where username='user1' and password='user1'";
$result=mysql_query($dacode) or die('Error, query failed');

if (mysql_num_rows($result) > 0) {
   while ($row=mysql_fetch_assoc($result)){
      if ($_POST['city'] == $row['city']) {
         echo 'FOUND IT';
      }
   }
} else {
   echo 'no rows found matching your query';
}

 

If running this code returns 'no rows found matching your query' it means your query isnt returning any results.. and you need to look at your query a little closer..

Also try:

echo mysql_num_rows($result);

Link to comment
https://forums.phpfreaks.com/topic/186022-while-loop-problem/#findComment-982324
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.