Page 1 of 1

SELECT COUNT giving 0 HELP

Posted: Thu Mar 05, 2009 10:48 am
by MIGO
hey guys, I am doing a control list using ip's so I want to count how many ip's with the same number do I have in my database equal to the person that is submiting the form in my webpage.

So im using this code:

Code: Select all

$ip = $_SERVER['REMOTE_ADDR'];
 
$checkin_ip = ('SELECT COUNT(ip_address) FROM `spam` WHERE `ip_address` = "$ip"'); 
 
$ipget = mysql_query($checkin_ip);
$ipid = mysql_fetch_array($ipget);
$ipid = $ipid[0];
basically I am getting the number 0, I am doing this test with my own ip and I have 11 not 0 :X

when I do print $ip; he gives me my ip but it is not working the query I do not know what is going on.

help me with the code to extract correctly the amount from the query.

cheers

Re: SELECT COUNT giving 0 HELP

Posted: Thu Mar 05, 2009 11:11 am
by jayshields
What data type is the ip_address field? If it's not VARCHAR you'll need to get rid of your double quotes.

Re: SELECT COUNT giving 0 HELP

Posted: Thu Mar 05, 2009 11:15 am
by MIGO
its varchar :X

Re: SELECT COUNT giving 0 HELP

Posted: Thu Mar 05, 2009 11:23 am
by MIGO
Problem solved basically the error was in this detail in red(bold):

$checkin_ip = ("SELECT COUNT(ip_address) FROM `spam` WHERE `ip_address` = '$ip'");

I've copy the query from mysql :X and did not change that ;)

Re: SELECT COUNT giving 0 HELP

Posted: Thu Mar 05, 2009 11:24 am
by miro_igov
Try this:

Code: Select all

echo '"$ip"';
and the result will be "$ip", i guess you do not save the word "$ip" in your spam table.

When you surround variable in apostrophe it does not evaluate it, if you surround in quotes it will do. Apostrophes are used for literal strings.


You can try this:

Code: Select all

$ip = $_SERVER['REMOTE_ADDR'];
 
$checkin_ip = ('SELECT COUNT(ip_address) FROM `spam` WHERE `ip_address` = "'.$ip.'"');
 
$ipget = mysql_query($checkin_ip);
$ipid = mysql_fetch_array($ipget);
$ipid = $ipid[0];

Re: SELECT COUNT giving 0 HELP

Posted: Thu Mar 05, 2009 12:48 pm
by BomBas
You should try this one:

Code: Select all

 
$ip = $_SERVER['REMOTE_ADDR'];
 
$checkin_ip = 'SELECT COUNT(ip_address) AS addresses FROM `spam` WHERE `ip_address` = "'. $ip. '"';
 
$ipget = mysql_query( $checkin_ip );
$ipid = mysql_fetch_assoc( $ipget );
$ips = $ipid['addresses'];
 

Re: SELECT COUNT giving 0 HELP

Posted: Thu Mar 05, 2009 1:01 pm
by jayshields
Yeah, what they said. Sorry, I should've spotted that!