And for the latest set of code I can see where the problem is so you get the error that you get:
$result = mysql_query ("SELECT * FROM counter WHERE IP = $newIP");
$row = mysql_fetch_array($result);
If there is no record of somebody visiting before from that IP (i.e. visits for the first time) the $result will return an empty array and thus will be the MySQL result resource...
I would check if there is a result at all like
if(mysql_num_rows($result)!=0){
$row = mysql_fetch_array($result);
$IP = $row['IP'];
$timestamp = $row['timestamp'];
}
this way you avoid this little error.
Will it perform like you need it I don't know (I haven't seen your entire code)...