Get DATETIME in php and post it to MySQL for transaction consistency

后端 未结 3 993
一向
一向 2020-12-31 17:41

Here is a link to the reason behind this question: NOW() for DATETIME InnoDB Transaction guaranteed?

So to ensure a single transaction with any number of queries (

相关标签:
3条回答
  • 2020-12-31 18:15

    You can do:

    <?
    $now = time();
    $sql = "INSERT INTO table SET field=FROM_UNIXTIME($now)";
    $sql2 = "INSERT INTO table SET field=FROM_UNIXTIME($now)";
    ...
    

    This avoids any possible issues of timezones, or local date formats.

    0 讨论(0)
  • 2020-12-31 18:16

    Do it in MySQL, so you don't involve PHP at all:

    select @now := now();
    
    insert into .... values (@now)
    select from ... where x=@now
    etc....
    
    0 讨论(0)
  • 2020-12-31 18:21

    One way could be:

    <?php
    $nowFormat = date('Y-m-d H:i:s');
    $sql = "INSERT INTO table SET field='$nowFormat'";
    $sql2 = "INSERT INTO table SET field='$nowFormat'";
    ...
    
    0 讨论(0)
提交回复
热议问题