duanqilupinf67040 2013-01-17 14:34
浏览 78
已采纳

在日期(日期)更改时添加新行 - MySQL,PHP

I need some support as I'm not so skilled in PHP and MySQL like all in this board :) So, thanks in advance.

I'm storing data from an external parsed XML feed with a PHP script. This script update MySQL database with a CRON Job twice a day. This is a snipped of such PHP script:

mysql_query("UPDATE my_table SET amount=$jackpot->amount WHERE gameId = '".$jackpot->gameId."'")
or die(mysql_error());

This update a table and a row in my database. This table is structured like this (columns):

gameId | gameName | amount | currency | date

where the "date" column is in DATE format (dd/mm/yyyy hh:mm), CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.

What I need... I need, yes, to update this table twice a day but I need also to INSERT a new row with the same columns at DATE change (every day, new day->new row).

I'm thinking in something similar, in two words (no coding):

if today --> update rows
elseif date change (another day, so "tomorrow") --> INSERT new row with updated data (collected from the XML feed)

How can I do this? Have I to check the current data with a MySQL query and then INSERT a new row via PHP? Or can I skip the query with a bunch of PHP lines?

Sorry, but I'm a little bit confused... Any help would be really appreciated!

Again, thanks to all!

  • 写回答

3条回答 默认 最新

  • duanliaolan6178 2013-01-17 14:46
    关注

    I'd suggest keeping it simple: add a new dupedate field to store ONLY the date of insertion. Put a unique key on it, then it's a simple matter of:

    INSERT INTO (gameID, gameName, ammount, currency, date, dupedate) VALUES (...)
    ON DUPLICATE KEY UPDATE amount=xxx
    

    If you "insert" a record with the same date as an existing record, you get an update. If it's a new date (e.g. 'tomorrow'), you'll get a new record instead.

    This also has the advantage of not being a racey as your initial "fetch/check/update|insert" version. Such a process leaves open the possibility that some OTHER process may duplicate the logic and snipe away your new record from under you in between the time you fetch the record and when you do the insert/update.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)