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:

我有这个php脚本每小时将数据保存到数据库中,我想知道如何在保存数据时添加时间戳。(另外,我应该在phpMyAdmin中输入长度/值)表中的列是:

Number, Name, Type, Null, Default

数字,名称,类型,Null,默认值

1, Timestamp, timestamp, No, CURRENT_TIMESTAMP

1, Timestamp, Timestamp, No, CURRENT_TIMESTAMP

2, BTC, float, Yes, NULL

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);    
?>

4 个解决方案

#1


2

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

由于您的列默认为CURRENT_TIMESTAMP,因此只需完全取出该字段——它将获得默认值

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.

请记住,如果这些是不同的机器,而且时间不同步,那么在数据库上的时间将会比在webserver上的时间长。

更多相关文章

  1. mysql查询一条工单时间需要10秒。优化sql语句得以解决。
  2. Spring Security ACL使用MySQL配置与数据库脚本
  3. MySql查询脚本,每月统计活动用户。
  4. MySQL导出和导入SQL脚本
  5. 如何从mysql datetime列返回转换后的时间格式?
  6. 如何将休眠时间戳映射到MySQL BIGINT?
  7. 为什么这个查询需要很长时间才能执行
  8. 在时间戳和相应的值中选择每天的最后一个条目。
  9. 如何在php中测量mysql时间,sql查询的时间和/或负载?

随机推荐

  1. 深入理解Java中的迭代器
  2. 再谈多态
  3. Java面试题全集(3)
  4. 03 Linux的常用命令
  5. 你可能不知道的15个 Git 命令[每日前端夜
  6. 能用 CSS 能播放声音吗?[每日前端夜话0xF1
  7. Java面试题全集(2)
  8. 15个 Vue.js 高级面试题[每日前端夜话0xF
  9. 英文都不懂.碰都没碰过编程软件!是心理的
  10. Volatitle的作用