0

Any idea why this wont work? I have a feeling its the row name I have given it. It doesnt echo anything

$result = mysql_query("SELECT UNIX_TIMESTAMP(datetime) FROM voters WHERE ip='$ip'") or die(mysql_error());

while($row = mysql_fetch_array($result)) {
   $unixtimestamp = $row['UNIX_TIMESTAMP(datetime)'];
   echo $unixtimestamp;
}
1
  • What is the result of that code? Commented May 23, 2011 at 19:18

2 Answers 2

2

Nevermind, datetime is not a reserved word, but I still highly recommend not using it and choosing a better column name. :), but the below still stands.

Alias the column, makes pulling it out in the array easier later on:

$result = mysql_query("SELECT UNIX_TIMESTAMP(`datetime`) as voted_on FROM voters WHERE ip='$ip'") or die(mysql_error());

while($row = mysql_fetch_array($result)) {
    $unixtimestamp = $row['voted_on'];
    echo $unixtimestamp;
}

Should work.

Sign up to request clarification or add additional context in comments.

3 Comments

Can I ask why I shouldnt use datetime? Just out of curiosity
DATETIME is a column definition when creating tables. As such it can add confusion etc. Its is better to avoid those type of column names. IE the line for the table definition would be: datetime DATETIME NOT NULL, . Looks weird and if MySQL later on deems it a reserved word (no clue if they would) it would break your app.
OK I will change it, thanks for the great answer. And the code works great, thanks :D
1

First, I'd recommend against using datetime as a column name in mysql.

If I had a DATETIME field called vote_time, I'd do it like this:

SELECT UNIX_TIMESTAMP(vote_time) AS unix_vote_time FROM voters WHERE ip='$ip'

Then you'd access it via

$unixtimestamp = $row['unix_vote_time'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.