Welcome Guest, Not a member yet? Register   Sign In
Query build - insert NOW()
#1

How would you go about having "NOW()" in insert?

The current insert doc:
PHP Code:
$data = array(
 
       'title' => 'My title',
 
       'name' => 'My Name',
 
       'date' => 'My date'
);

$this->db->insert('mytable'$data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date') 

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?
Reply
#2

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 )
Reply
#3

(11-23-2016, 05:26 AM)InsiteFX Wrote: Look at the CodeIgniter date_helper in the Users Guide.

There is a Now() method.

That is not the same as a database's NOW(), which the OP explained.
Reply
#4

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
Reply
#5

(11-24-2016, 12:36 AM)sv3tli0 Wrote: 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..

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
Reply
#6

(This post was last modified: 11-24-2016, 02:58 AM by sv3tli0.)

(11-24-2016, 01:44 AM)AmitMY Wrote:
(11-24-2016, 12:36 AM)sv3tli0 Wrote: 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..

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

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB