5 Replies - 1600 Views - Last Post: 28 March 2011 - 03:28 AM Rate Topic: -----

#1 RyanRobinson  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 227
  • Joined: 31-March 10

Validate data against many database records

Posted 27 March 2011 - 03:14 AM

Hi guys,

I am trying to get my If statement to check if the IP address of a user who visits a page matches an IP address in the MySQL database. At the moment, I can see the "Thank you" page as my IP address is the first record in the table. However, I have been using a proxy to test another IP address and it shows me the "Error" page. I think this has something to do with my query array only checking the first record and not the rest.

Here's the PHP code

<?php

	require_once 'db.php';
	
	$ip_user = $_SERVER['REMOTE_ADDR'];
	
	$query = "SELECT ip_add FROM signup";
	
	if(!mysql_query($query))
	{
		echo "<h4>Query failed</h4>";
	}
	
	$result = mysql_query($query);
	
	$rec = mysql_fetch_array($result);
	
	if($ip_user == $rec['ip_add'])
	{
		echo "You can download these ebooks.";
		echo "<p>Your IP: $ip_user</p>";
	}else {
		echo "Trying to get a free ride?";
		echo "<p>Your IP: $ip_user</p>";
	}


?>



Is This A Good Question/Topic? 0
  • +

Replies To: Validate data against many database records

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: Validate data against many database records

Posted 27 March 2011 - 07:38 AM

Check out this comment in the PHP documentation.
Was This Post Helpful? 0
  • +
  • -

#3 RyanRobinson  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 227
  • Joined: 31-March 10

Re: Validate data against many database records

Posted 27 March 2011 - 09:04 AM

Thanks for the reply.

My problem is not with the Server Remote address. What I need to do, is check whether a users IP matches one within the mysql table. If the users IP address matches one of the IPs in the table then I will display some content, and if it doesn't it will just output some message.

At the moment, the PHP code above only checks the first IP address in the table and I need it to check through every record. I am guessing by using loop?
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Validate data against many database records

Posted 27 March 2011 - 11:24 AM

If you want to just verify if there's a specific entry in your table you can use MySQL's function COUNT. This will return the amount of rows that matches the executed query. Also utilize MySQL's keyword WHERE that will do the matching for you instead of iterating the entire database.

Another function that will come in handy when comparing data in your database is LIKE.

<?php

	require_once 'db.php';
	
	$ip_user = $_SERVER['REMOTE_ADDR'];
	
	$query = "SELECT COUNT(ip_add) TOTAL FROM signup WHERE ip_add = $ip_user";
	
	if(!mysql_query($query))
	{
		die("<h4>Query failed</h4>");
	
	}
	
	$result = mysql_query($query);
	
	$rec = mysql_fetch_assoc($result);
	
	if((int)$rec['TOTAL'] > 0)
	{
		echo "You can download these ebooks.";
		echo "<p>Your IP: $ip_user</p>";
	}else {
		echo "Trying to get a free ride?";
		echo "<p>Your IP: $ip_user</p>";
	}


?>

Was This Post Helpful? 1
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: Validate data against many database records

Posted 27 March 2011 - 01:49 PM

Read about how to use MySQL with PHP.
Was This Post Helpful? 0
  • +
  • -

#6 RyanRobinson  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 227
  • Joined: 31-March 10

Re: Validate data against many database records

Posted 28 March 2011 - 03:28 AM

Thanks codeprada,

I actually read about that method in my book but I completely forgot about it. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1