jack bauer
jack bauer

Reputation: 41

How to get and compare row values in PHP/MySQL?

I would like to know what code I would need to use to get the values from a row (which would naturally have data from different columns)

I would like to check if the IP address is the same as the Poll ID. As you would expect the IP address is stored in one column, and the poll id in a next column.

  $q = mysql_query("SELECT * FROM logs");

  while($row = mysql_fetch_array($q))
  {
      if(($ip = $_SERVER['REMOTE_ADDR']) == $row['ip'])
      {
         $duplicateIP = true;
       }//end if

      if(($row['poll_id'] == 1))
      {
         $duplicatePoll = true;
       }//end if
   }//end while

I realised this code won't work as it will return the poll id to be true as long as it is equal to 1. How do I ensure that it will only return to be true if that poll id matches the IP address? Thanks.

Upvotes: 1

Views: 1038

Answers (1)

Andrzej Reduta
Andrzej Reduta

Reputation: 767

Instead of:

if(($ip = $_SERVER['REMOTE_ADDR']) == $row['ip'])
{
    $duplicateIP = true;
}//end if

if(($row['poll_id'] == 1))
{
    $duplicatePoll = true;
}//end if

You should write:

if($_SERVER['REMOTE_ADDR'] == $row['ip'] && $row['poll_id'] == 1){

    $duplicateIP = true;

}//end if

Or change your query:

$q = mysql_query(
    "SELECT * FROM logs WHERE ip = '".$_SERVER['REMOTE_ADDR'] .
    "' and poll_id = '".$iPollid."' "
);

Upvotes: 1

Related Questions