Page 1 of 1

foreach

Posted: Thu Sep 23, 2010 4:35 am
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

Re: foreach

Posted: Thu Sep 23, 2010 4:46 am
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";
}