0

OK, so I want to know this:

Is my IP in my database? If yes, find the timestamp of entry.

So far I have this:

$ip = $_SERVER['REMOTE_ADDR'];
$myip = mysql_query("SELECT * FROM votes WHERE ip = '$ip'");

If(mysql_num_rows($myip) > 0){
//find timestamp and name it as a variable e.g $ipTime

Thats as far as I've got, and for the life of me, I'm a little stuck, anyone help me?

Spudley
  • 166,037
  • 39
  • 233
  • 307
Ricki
  • 933
  • 2
  • 15
  • 33
  • 1
    [I wish my IP address could contain an apostrophe](http://xkcd.com/327/). Maybe sometime in the future? –  Oct 13 '11 at 13:51
  • [mysql_fetch_assoc](http://www.php.net/mysql_fetch_assoc) But for new code, please use [PDO](http://www.php.net/PDO) and its prepared statements – Wrikken Oct 13 '11 at 13:52
  • 1
    Read up on the basic usage of MYSQL. – Alex Turpin Oct 13 '11 at 13:55
  • made me chuckle. Describes my everyday understanding of MySQL. @Wrikken thanks for the link. – Ricki Oct 13 '11 at 14:18
  • do you have a timestamp field in your DB table? Perhaps you could show us the structure of your DB table so we can see enough info to help you? – Spudley Oct 13 '11 at 14:28
  • i do have a time stamp which is automatically updated – Ricki Oct 13 '11 at 15:03

1 Answers1

2

This should do the trick, it should get the timestamp and convert it to a human readable format, you may also want to look up the PHP date() function to adjust the $date_format to your liking which can be found here: http://php.net/manual/en/function.date.php

$ip = $_SERVER['REMOTE_ADDR']; // Fetch IP

$query = mysql_query("SELECT `timestamp` FROM `votes` WHERE (`ip` = '$ip')") or die(mysql_error()); // Queries IP

$amount = mysql_num_rows($query); // Amount of times listed (rows)

if ($amount > 0) { // If exists

    $row = mysql_fetch_array($query);

    $timestamp = $row['timestamp'];

    $date_format = 'm F \a\t g:ia';
    $date = date("$date_format", $timestamp); // Converts timestamp

    echo("Your IP Address (".$ip.") is already listed at ".$date."");

} else {

    echo("Your IP Address is not currently listed");

}
no.
  • 2,356
  • 3
  • 27
  • 42
  • thanks! Very in depth. I know alot about the date function in PHP but never really thought of this approach towards a timestamp. – Ricki Oct 13 '11 at 15:08