Insert current date/time using now() in a field using MySQL/PHP

后端 未结 9 1220
不知归路
不知归路 2021-02-01 01:00

Since MySQL evidently cannot automatically insert the function now() in a datetime field in adding new records like some other databases, based on comments, I\'m explicitly tryi

相关标签:
9条回答
  • 2021-02-01 01:34

    Like Pekka said, it should work this way. I can't reproduce the problem with this self-contained example:

    <?php
        $pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    
        $pdo->exec('
            CREATE TEMPORARY TABLE soFoo (
                id int auto_increment,
                first int,
                last int,
                whenadded DATETIME,
                primary key(id)
            )
        ');
        $pdo->exec('INSERT INTO soFoo (first,last,whenadded) VALUES (0,1,Now())');
        $pdo->exec('INSERT INTO soFoo (first,last,whenadded) VALUES (0,2,Now())');
        $pdo->exec('INSERT INTO soFoo (first,last,whenadded) VALUES (0,3,Now())');
    
        foreach( $pdo->query('SELECT * FROM soFoo', PDO::FETCH_ASSOC) as $row ) {
            echo join(' | ', $row), "\n";
        }
    

    Which (currently) prints

    1 | 0 | 1 | 2012-03-23 16:00:18
    2 | 0 | 2 | 2012-03-23 16:00:18
    3 | 0 | 3 | 2012-03-23 16:00:18
    

    And here's (almost) the same script using a TIMESTAMP field and DEFAULT CURRENT_TIMESTAMP:

    <?php
        $pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    
        $pdo->exec('
            CREATE TEMPORARY TABLE soFoo (
                id int auto_increment,
                first int,
                last int,
                whenadded TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                primary key(id)
            )
        ');
        $pdo->exec('INSERT INTO soFoo (first,last) VALUES (0,1)');
        $pdo->exec('INSERT INTO soFoo (first,last) VALUES (0,2)');
        sleep(1);
        $pdo->exec('INSERT INTO soFoo (first,last) VALUES (0,3)');
    
        foreach( $pdo->query('SELECT * FROM soFoo', PDO::FETCH_ASSOC) as $row ) {
            echo join(' | ', $row), "\n";
        }
    

    Conveniently, the timestamp is converted to the same datetime string representation as in the first example - at least with my PHP/PDO/mysqlnd version.

    0 讨论(0)
  • 2021-02-01 01:37

    now()

    worked for me . but my field type is date only and yours is datetime. i am not sure if this is the case

    0 讨论(0)
  • 2021-02-01 01:39

    Currently, and with the new versions of Mysql can insert the current date automatically without adding a code in your PHP file. You can achieve that from Mysql while setting up your database as follows:

    Now, any new post will automatically get a unique date and time. Hope this can help.

    0 讨论(0)
  • 2021-02-01 01:44

    The only reason I can think of is you are adding it as string 'now()', not function call now().
    Or whatever else typo.

    SELECT NOW();
    

    to see if it returns correct value?

    0 讨论(0)
  • 2021-02-01 01:48

    These both work fine for me...

    <?php
      $db = mysql_connect('localhost','user','pass');
      mysql_select_db('test_db');
    
      $stmt = "INSERT INTO `test` (`first`,`last`,`whenadded`) VALUES ".
              "('{$first}','{$last}','NOW())";
      $rslt = mysql_query($stmt);
    
      $stmt = "INSERT INTO `users` (`first`,`last`,`whenadded`) VALUES ".
              "('{$first}', '{$last}', CURRENT_TIMESTAMP)";
      $rslt = mysql_query($stmt);
    
    ?>
    

    Side note: mysql_query() is not the best way to connect to MySQL in current versions of PHP.

    0 讨论(0)
  • 2021-02-01 01:48

    Just go to the column whenadded and change the default value to CURRENT_TIMESTAMP

    0 讨论(0)
提交回复
热议问题