1

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.

2
  • 3
    Unless you are using $ip elsewhere there is no need to do that variable assignment in your if statement. Commented Apr 10, 2014 at 13:22
  • This? SELECT * FROM logs WHERE IP = $_SERVER['REMOTE_ADDR']; Commented Apr 10, 2014 at 13:29

1 Answer 1

1

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."' "
);
Sign up to request clarification or add additional context in comments.

3 Comments

Thumbs up for putting the IP and PoolID on the query. You can also do something like "SELECT 1 AS exists FROM logs WHERE ip = '".$_SERVER['REMOTE_ADDR']."' and poll_id = '".$iPollid." LIMIT 1;'. This will return one row with a 1 in the array index "exists". It that comes empty, it does not exists on the table.
i believe LIMIT 1 is not needed. There should be 'unique' index on ip+poll_id
Yep. But since we don't have the schema, plan for the worst... hehe (also the table is called log, so you can expect to have multiple rows for the same ip-pollid pair). Also would be nice to add an index to that pair of columns. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.