Query build - insert NOW() |
How would you go about having "NOW()" in insert?
The current insert doc: PHP Code: $data = array( And when wanting to insert "NOW()" for timestamp/datetime, you have to do it using PHP PHP Code: date("Y-m-d H:i:s") Problem? Can be. If the database time is different than the server time, Or, you have multiple servers using the same DB, and can sometimes go out of sync in milliseconds (and with time in seconds) - there are sync methods, not the point Or, latency between servers to the DB is different, Time will not be correct. It can have very very minor artifacts. There is a solution for updating, which is using the "Set" function. So how would you go about that?
Look at the CodeIgniter date_helper in the Users Guide.
There is a Now() method. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Just to mark something..
Usually it's better to generate time including now , for insert or select within your PHP code. It's not good to relate on both DB time and PHP time as they may have different settings and with that the time you are searching or inserting can mismatch.. Best VPS Hosting : Digital Ocean
(11-24-2016, 12:36 AM)sv3tli0 Wrote: Just to mark something.. Well, I am not using both. I am using only DB time both in my insert (using CURRENT_TIMESTAMP) and in my update. Also, every "WHERE" I do that is related to time I use NOW or date methods, and for selecting I use UNIX_TIMESTAMP to get it back using a timestamp. So I think I got all bases covered, for multiple timezones, multiple servers, and time differences, except for the insert method where if I want to insert NOW() I currently can't (11-24-2016, 01:44 AM)AmitMY Wrote:(11-24-2016, 12:36 AM)sv3tli0 Wrote: Just to mark something.. Nice, then from documentation you can see that "All values are escaped automatically producing safer queries." so.. in that case you can just write a plain query Insert .. instead of using the query builder. Or.. again into the Documentation you can see echo $this->db->set('title', 'My Title')->get_compiled_insert('mytable', FALSE); // Produces string: INSERT INTO mytable (`title`) VALUES ('My Title') echo $this->db->set('content', 'My Content')->get_compiled_insert(); // Produces string: INSERT INTO mytable (`title`, `content`) VALUES ('My Title', 'My Content') You have the option to use $this->db->set with 3rd param to false (which will prevent escaping of your NOW()). Best VPS Hosting : Digital Ocean
|
Welcome Guest, Not a member yet? Register Sign In |