foreach

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Anant
Forum Commoner
Posts: 66
Joined: Wed Jul 14, 2010 11:46 am

foreach

Post by Anant »

Hi,

It's very simple question but am not getting the right syntax -

Basically - i have a table with the column for ids.
Now when user enters that id in the form - i want that entered id to be matched against all the ids available in that table in database -

Code: Select all

mysql_select_db($db,$link);
$query = "select xid from test";
$result = mysql_query($query, $link) or die(mysql_error());
$row = mysql_fetch_array($result);

foreach ($row as $val)
{
    if($val == $CustomerEnteredID)
   {
   // DO THIS
   }
   else 
  { 
   echo "not found";
  }
}
But i can not iterate through the foreach loop - it just match it with the first value and display "not found"...

I know it's something simple am missing - can anyone please shed some light on it.

Thanks
amargharat
Forum Commoner
Posts: 82
Joined: Wed Sep 16, 2009 2:43 am
Location: Mumbai, India
Contact:

Re: foreach

Post by amargharat »

No need to iterate, just use following code

Code: Select all

mysql_select_db($db,$link);
$query = "select xid from test where xid=" . $CustomerEnteredID;
$result = mysql_query($query, $link) or die(mysql_error());
$nr = mysql_num_rows($result);
if($nr > 0)
{
      //do this
}else
{
       echo "Not Found";
}
Post Reply