Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: DBI placeholders, bind_values, and SQL_DATE

by tune (Curate)
on Aug 16, 2001 at 20:11 UTC ( [id://105428]=note: print w/replies, xml ) Need Help??


in reply to DBI placeholders, bind_values, and SQL_DATE

If you want to insert the current date every time, why don't you do this:
$query="INSERT INTO test ( Name, EntryDate ) VALUES ( ?, now() )";
or
$query="INSERT INTO test ( Name, EntryDate ) VALUES ( ?, CURRENT_TIMES +TAMP )";

--
tune

Replies are listed 'Best First'.
Re: Re: DBI placeholders, bind_values, and SQL_DATE
by christopherbarsuk (Acolyte) on Aug 16, 2001 at 23:21 UTC
    Thanks, but I'm writing a generic function -- one to which I can pass any number of fields/values of various types -- so I needed a way to generalize the SQL.

    (btw, I've used the first suggestion above -- rather than pass the value 'Now()' to my generic insert_into function, I convert 'Now()' to the properly formatted date value for the database (MySQL))

      Another option... I don't know if MySQL has the NVL function, which returns the first argument if it's not null, and the second argument otherwise. With Oracle, you could do something like this:
      $insert_sth = $dbh->prepare(<<"EndOfSQL"); INSERT INTO members (member_id, name, date_joined) VALUES (?, ?, NVL(?, SysDate)) EndOfSQL $insert_sth->execute(1, 'Bob', '2001-08-15'); # Bob actually joined y +esterday $insert_sth->execute(2, 'Tom', undef); # Tom is joining now
      So, you can bind a specific date in the execute, or you can bind undef and let the database specify the current date.

        Along this same sort of line you could set a default values in the database itself and then don't even put it in the SQL statement at all unless you don't want the current time for certain queries.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://105428]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-19 15:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found