Question

I have this php script to save data to a database hourly, and I was wondering how to add a timestamp when the data is saved. (also, what should I put as Length/Values in phpMyAdmin) The columns in my table are:

Number, Name, Type, Null, Default

1, Timestamp, timestamp, No, CURRENT_TIMESTAMP

2, BTC, float, Yes, NULL

3, USD, float, Yes, NULL

My script:

<?php
$json_url = "https://crypto-trade.com/api/1/ticker/dvc_btc";
$json_data = file_get_contents($json_url);
$json_feed = json_decode($json_data);
$DVCdata = $json_feed->data;
$DVCask = $DVCdata->min_ask;
$json_url1 = "https://api.bitcoinaverage.com/ticker/USD";
$json_data1 = file_get_contents($json_url1);
$json_feed1 = json_decode($json_data1);
$BTCask = $json_feed1->ask;
$DVC_USD = $BTCask * $DVCask;
$DVCround = round($DVC_USD, 8);
$connection = mysqli_connect("mysql.serversfree.com",user,pass,database); 

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($connection,"INSERT INTO database (Timestamp, BTC, USD)
VALUES (???, '$DVCask', '$DVCround')");

mysqli_close($connection);    
?>
Was it helpful?

Solution

As your column defaults to CURRENT_TIMESTAMP just take out that field entirely - it will get the default value

mysqli_query($connection,"INSERT INTO database (BTC, USD) VALUES ('$DVCask', '$DVCround')");

Bear in mind it will be the time on the database rather than the webserver if those are different machines and the time is not in sync.

OTHER TIPS

try NOW()

mysqli_query($connection,"INSERT INTO database (Timestamp, BTC, USD)
VALUES (NOW(), '$DVCask', '$DVCround')");

The value will be formatted as such : 2013-10-12 11:18:02. depending on your field type, only the date or the date and the time will be saved. To save only the date, the field type should be DATE, if you want to keep the hour as well, use DATETIME

for the other fields, as they seem to contain dollar values you might want to use decimal fields instead? keep in mind that in the length attribute of the field, you have to enter two numbers, the first being the total number of digits, the second the number of digits after the comma, like 5,2

Set the Timestamp field as datetime NOT NULL DEFAULT NOW(). That will get the current date/time for you.

More details here: http://www.w3schools.com/sql/func_now.asp

If you want to use the php synthax you could define a variable $timestamp = date('Y-m-d H:i:s'); and after that use:

<?php
$json_url = "https://crypto-trade.com/api/1/ticker/dvc_btc";
$json_data = file_get_contents($json_url);
$json_feed = json_decode($json_data);
$DVCdata = $json_feed->data;
$DVCask = $DVCdata->min_ask;
$json_url1 = "https://api.bitcoinaverage.com/ticker/USD";
$json_data1 = file_get_contents($json_url1);
$json_feed1 = json_decode($json_data1);
$BTCask = $json_feed1->ask;
$DVC_USD = $BTCask * $DVCask;
$DVCround = round($DVC_USD, 8);
$timestamp = date('Y-m-d H:i:s');
$connection = mysqli_connect("mysql.serversfree.com",user,pass,database); 

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($connection,"INSERT INTO database (Timestamp, BTC, USD)
VALUES ('$timestamp', '$DVCask', '$DVCround')");

mysqli_close($connection);    
?>

Cheers

If you defined your column datatype as CURRENT_TIMESTAMP, then there is no need of adding value in query.

If you have not used then,

$timestamp = date('Y-m-d H:i:s');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top