tags:

views:

2370

answers:

5

Thanks for looking. All helpful answers/comments are up voted.

In php, you can use NOW() like this:

mysql_query("INSERT INTO tablename (id,      value,      time_created) 
                            VALUES ('{$id}', '{$value}', NOW())");

How can I do the same thing in PDO. When I bind like this, I get an error:

$stmt->bindParam(':time_added', NOW(), PDO::PARAM_STR);

Is it the PDO:PARAM_STR?

+5  A: 

Just do this:

mysql_query("INSERT INTO tablename (id, value, time_created)  
             VALUES ('$id', '$value', NOW())");

Although you have to have the right column-type. date or time alone, won't work if I recall correctly; it has to be datetime or maybe timestamp?

Ollie Saunders
Correct, and the same principle applies when porting to PDO
David Caunt
I seem to be getting an error in PDO though when I do this: `$stmt->bindParam(':time_added', NOW(), PDO::PARAM_STR);` Is it the PDO:PARAM_STR?
Chris
@Ollie +1 for the mysql part. I'm now stuck with the PDO part.
Chris
You are getting an error because you are trying to bind NOW() as a query parameter, when NOW() is actually a mysql function, not a PHP function. You don't need to bind a parameter for it, assuming you're using the same SQL statement as above and still using mysql.
RMorrisey
Yeah I think RMorrisey is right. If you know how to bind $id and $value there's no more work to be done on the PDO side of things.
Ollie Saunders
How do you bind the value the function outputs then? bindValue? Why not say so straight away? Trying to tantalize me ;) I'm looking for help to learn :) not be mocked around :(
Chris
You don't! NOW() is part of the MySQL query just as "INSERT INTO" is.
Ollie Saunders
If you want to bind a PHP function return you might have to assign the return to a variable first. If I recall PDO does everything by reference because the implementors don't understand that PHP isn't actually like C.
Ollie Saunders
Ask a separate question if you still have difficulty.
Ollie Saunders
Ok, I think I'm getting your point about it being part of the query string. Makes sense I suppose since it's not user input. Will try it and come back with a new question if needed.
Chris
Although the general concept is correct (`NOW()` is a MySQL function and does not need to be bound on the PHP side), it's always a bad idea to pass variables directly into a SQL string... That's one reason bound parameters are there for.
Stefan Gehrig
+1  A: 

other than NOW() i also utilize the "timestamp" type column and set its default to CURRENT_TIMESTAMP .. so i just pass nothing for that field and time is automatically set. maybe not exactly what ur looking for.

Sabeen Malik
+2  A: 

Because nobody has explicitly answered the question (although the correct answer can be extracted from the comments to Ollie Saunders's answer), I'll add the correct answer for the sake of completeness.

$stmt = $pdoDb->prepare('INSERT INTO tablename (id, value, time_created) VALUES (:id, :value, NOW())');
// either bind each parameter explicitly 
$stmt->bindParam(':id', $id); // PDOStatement::bindValue() is also possibly
$stmt->bindParam(':value', $value);
$stmt->execute();
// or bind when executing the statement
$stmt->execute(array(
    ':id'    => $id,
    ':value' => $value
));
Stefan Gehrig
+1  A: 

WTF? none of the answers solve the question as I see it!

So there are some of my findings: there is NO WAY how to force PDO to pass MySQL function call as a query value - so there is no way to do simple wrapper that will be able to use NOW() or any other function as passed values. Every time you need something like that, you need manulay change the query, so the function call is part of the query string. :-(

I'm using funcion that tests given values for MySQL function I am using and modifies the query itself, but it is not a good solution to my opinion... :-}

B.F.U.
A: 

this maybe be usefull to some of you maybe not but I was confronted with the same problem as Ollie Saunders was. I'm pretty new to php/mysql an most of all PDO. What I found working for me is the following:

$active = 0;      
$id = NULL;
$query = "INSERT 
        INTO tbl_user(ID_user, firstname, lastname, email, password, active, create_date)
        VALUES (?,?,?,?,?,?,NOW())";

if($stmt=$this->conn->prepare($query)) {
$stmt->bind_param('issssi', $id, $firstname, $lastname, $email, $password, $active);
$stmt->execute();
}

and guess what it works! Hope to have helped here. Any comments are welcome. try it and tell me if it worked for you and if you have any additions.

Chris Tailor