When I issue the following query in the mysql console for my database, I get the following:
mysql> SELECT ipaddr FROM ipposts WHERE ipaddr=2149856614;
+------------+
| ipaddr |
+------------+
| 2149856614 |
| 2149856614 |
| 2149856614 |
+------------+
3 rows in set (0.00 sec)
However, the following codesnippet from my php:
$query = "SELECT ipaddr FROM ipposts WHERE ipaddr=2149856614";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
if($rows == 0)
return addIP($ip);
Calls addIP() everytime. Why is this?
EDIT I used mysql_error() to dump out one that I've never seen before "TABLE 'ipposts' was not locked with LOCK TABLES", I added a LOCK READ,WRITE and an UNLOCK in my function and now it works. I'm not sure why mysql wanted my to lock this in this function?
Did you try:
$query = "SELECT ipaddr FROM ipposts WHERE ipaddr=2149856614";
$result = mysql_query($query);
if ($result) {
$rows = mysql_num_rows($result);
if($rows == 0)
return addIP($ip);
}
Anyway, because of you don't need to get records but only their count, you could use a different query:
SELECT COUNT(ipaddr) FROM ipposts
WHERE ipaddr=2149856614
and check if it's zero (result, not number of rows) or not
Try $query = "SELECT ipaddr FROM ipposts WHERE ipaddr='2149856614'";
if the ipaddr
column is defined as a varchar
You'll need to use a BIGINT
in order to store a number this large, or if you absolutely know that you won't have negative values, you can use UNSIGNED INT
which will allow storing of numbers 0-4294967295.
Also, you should be preforming error checks in order to make sure there was no error in your query.
Also, you shouldn't be using an INT
type field to store IP addresses. For instance,
111.222.333.44
111.222.33.344
Both will store the same in your database. You should be storing them as a string, or extract a longip