insert (now(),now(),....)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Bedford

    insert (now(),now(),....)

    I've a query with insert records in a table.

    I've 2 fields: DateTimeInserti on and DateTimeLastMod ification.

    When I create the record, I must have those 2 fields set the same value.

    Now I've this

    Insert into article values(now(), now(),....)

    but what appens if the time changes between the 2 now calls ?

    It's there any way to be sure to set the same value ?

    Thanks

    Bob

    PS: I've no access to MySQL NG.

  • Kristian Köhntopp

    #2
    Re: insert (now(),now(),.. ..)

    Bob Bedford wrote:[color=blue]
    > Insert into article values(now(), now(),....)
    > but what appens if the time changes between the 2 now calls ?[/color]

    In MySQL, it shouldn't, because MySQL guarantees a single statement to be
    atomic, even if no other transactional mechanism is being used.

    If you decide to use user variables, you'd be on the safe side, anyway.

    http://dev.mysql.com/doc/mysql/en/ex...variables.html,



    "MySQL supports user variables as of version 3.23.6. You can store a value
    in a user variable and refer to it later, which allows you to pass values
    from one statement to another. User variables are connection-specific. That
    is, a variable defined by one client cannot be seen or used by other
    clients. All variables for a client connection are automatically freed when
    the client exits."

    mysql> select version();
    +------------+
    | version() |
    +------------+
    | 4.0.18-Max |
    +------------+
    1 row in set (0.00 sec)
    mysql> select @t := now();
    +---------------------+
    | @t := now() |
    +---------------------+
    | 2005-03-21 11:10:17 |
    +---------------------+
    1 row in set (0.00 sec)
    mysql> select @t;
    +---------------------+
    | @t |
    +---------------------+
    | 2005-03-21 11:10:17 |
    +---------------------+
    1 row in set (0.00 sec)
    mysql> create table tab ( time_a datetime, time_b datetime);
    Query OK, 0 rows affected (0.24 sec)

    mysql> insert into tab (time_a, time_b ) values ( @t, @t);
    Query OK, 1 row affected (0.00 sec)

    Kristian

    Comment

    • Azeus

      #3
      Re: insert (now(),now(),.. ..)

      Bob Bedford wrote:[color=blue]
      > I've a query with insert records in a table.
      >
      > I've 2 fields: DateTimeInserti on and DateTimeLastMod ification.
      >
      > When I create the record, I must have those 2 fields set the same value.
      >
      > Now I've this
      > Insert into article values(now(), now(),....)
      >
      > but what appens if the time changes between the 2 now calls ?
      >
      > It's there any way to be sure to set the same value ?[/color]

      Try setting one date variable and inserting it into all date fields you
      want to update with now();

      $time_now = date("Y-m-d H:i:s"); // the current date/time
      $query = "insert into test.test
      values(\"{$time _now}\",\"{$tim e_now}\",\"{$ti me_now}\")";

      mysql_query($qu ery) or die('Query error: ' . mysql_error());

      $query = "select * from test.test";
      $res = mysql_query($qu ery);

      while(($row = mysql_fetch_ass oc($res)) != null){
      echo " datetime1 = ".$row["date1"];
      echo " datetime2 = ".$row["date2"];
      echo " datetime3 = ".$row["date3"];
      echo "\n";
      }

      Comment

      Working...