Welcome Guest, Not a member yet? Register   Sign In
How to call NOW() mysql function using CI?
#1

[eluser]suba[/eluser]
$data=array('s_name'=>$name,'s_nric'=>$s_nric,'s_dob'=>$dob,'s_nationality'=>$nationality,'s_citizenship'=>$citizenship,'s_cob'=>$cob,'s_race'=>$race,'s_gender'=>$gender,'s_religion'=>$religion,'s_email'=>$email,'s_hobby'=>$hoppy,'s_class'=>$class,'s_reg_no'=>$regno,'s_academy_qualification'=>$entry,'s_age'=>$age,'s_lastschool'=>$last_school,'s_admission_category'=>$admission,'s_current_cca'=>$current_cca,'s_previous_cca'=>$previous_cca,'s_updatedate'=>NOW());

$nric=$this->uri->segment(3);
$where=array('s_nric'=>$nric);
$success=$this->db->update('student_data',$data,$where);

NOW() function shows error.
so how to call this function in CI?
#2

[eluser]John_Betong[/eluser]
[quote author="suba" date="1260437571"]
Code:
$data=array
(
   's_name'=>$name,
   's_nric'=>$s_nric,
   's_dob'=>$dob,
   's_nationality'=>$nationality,
   's_citizenship'=>$citizenship,
   's_cob'=>$cob,
   's_race'=>$race,
   's_gender'=>$gender,
   's_religion'=>$religion,
   's_email'=>$email,
   's_hobby'=>$hoppy,
   's_class'=>$class,
   's_reg_no'=>$regno,
   's_academy_qualification'=>$entry,
   's_age'=>$age,
   's_lastschool'=>$last_school,
   's_admission_category'=>$admission,
   's_current_cca'=>$current_cca,
   's_previous_cca'=>$previous_cca,
   [b][color=red]'s_updatedate'=>NOW()[/color][/b]
);
        
$nric=$this->uri->segment(3);
$where=array('s_nric'=>$nric);
$success=$this->db->update('student_data',$data,$where);
NOW() function shows error.
so how to call this function in CI?[/quote]
 
I searched for now() and mysql and came up with 70 results!
Here are the top two threads:

http://ellislab.com/forums/viewthread/136688/

 http://ellislab.com/forums/viewthread/130112/
 
 
#3

[eluser]dhenoer[/eluser]
NOW() is not php function, its mysql function.

You may use internal php function like date() and time().
if type of s_updatedate is date, than replace NOW() with date('Y-m-d')
if type of s_updatedate is datetime, than replace NOW() with date('Y-m-d H:iConfused')
if type of s_updatedate is int, than replace NOW() with time()

if you want native mysql function in query, use CI syntax like this:

$query = "UPDATE yourtable SET field1=NOW() WHERE id='anything'";
$this->db->query($query);

but the last method doesn't secure as CI doesn't automatically protect query string using backtic operator.
#4

[eluser]John_Betong[/eluser]
[quote author="dhenoer" date="1260446251"]NOW() is not php function, its mysql function.

You may use internal php function like date() and time().
if type of s_updatedate is date, than replace NOW() with date('Y-m-d')
if type of s_updatedate is datetime, than replace NOW() with date('Y-m-d H:iConfused')
if type of s_updatedate is int, than replace NOW() with time()

if you want native mysql function in query, use CI syntax like this:

$query = "UPDATE yourtable SET field1=NOW() WHERE id='anything'";
$this->db->query($query);

but the last method doesn't secure as CI doesn't automatically protect query string using backtic operator.[/quote]
 
If you cannot get the last method to work then try using one of the other solutions.
 
 
#5

[eluser]Mareshal[/eluser]
NOW() from SQL is same as time() from php...
#6

[eluser]Phil Sturgeon[/eluser]
[quote author="Mareshal" date="1260456241"]NOW() from SQL is same as time() from php...[/quote]

Not really, NOW() can return MySQL datetime, date or time depending on the field, whereas time() returns unix timestamp.

The only neat way to use NOW() with AR is this:

Code:
$this->db->set('field', 'NOW()', FALSE);
#7

[eluser]jedd[/eluser]
[quote author="Mareshal" date="1260456241"]NOW() from SQL is same as time() from php...[/quote]

The other really big and often over looked difference is that NOW() reports the time on the host running your database, and php's time() reports the time on the host(s) that run your web site.

While they may be the same machine, or they may be both NTP'd, neither is guaranteed if you're not hosting the box(es) yourself - so it's probably best to pick one source for your timestamps, and use that source consistently throughout your application. My gut feel is that the preferable source to trust is the database.




Theme © iAndrew 2016 - Forum software by © MyBB