Welcome Guest, Not a member yet? Register   Sign In
using mysql function in update function's config virable
#1

[eluser]杨帅[/eluser]
i use the following code to update a record in my database, but i want to use the mysql now() function in the config variable $data, the following syntaz output error, cuz php thought now() is a php function, and it cannot find it. if i surround quotes on now() as
Code:
'last_edit_time'=>'now()';
it also output error, cuz mysql thought "now()" is a string ,and it cannot be insert into a datetime collum in the database. anyone know how to solve this problem?
Code:
$data=array(
        'name'=>$name,
        'sex'=>$sex,
        'work_experience'=>$work_experience,
        'last_edit_time'=>now();
    );
            
        $username=$this->session->userdata('username');    
            
        $this->db->where('username', $username);
    $this->db->update('person', $data);
#2

[eluser]alpar[/eluser]
try to use the date() function or store timestamps in your database
#3

[eluser]座頭市[/eluser]
Syntax error: get rid of the semi-colon after now(). And as mentioned above, use the date() function.
#4

[eluser]杨帅[/eluser]
oh,the semi-colon is just a scribal error
the last_edit_time in mysql is of type datetime. i try date() and 'date()' , both failed
#5

[eluser]riddla[/eluser]
Code:
'last_edit_time' => date("Y-m-d H:i:s")
should help you out.
#6

[eluser]John_Betong[/eluser]
Hi 杨帅,

Here is how I update the last_edit_time by setting the MySqlPhp/table/field type, attributes and default settings:

Quote:
Field ==> last_edit_time
Type ==> timestamp
Attributes ==> ON UPDATE CURRENT_TIMESTAMP
Default ==> CURRENT_TIMESTAMP
 
Cheers,
 
John_Betong
 
 
#7

[eluser]杨帅[/eluser]
thank you john, i got it.
#8

[eluser]blackarma[/eluser]
Hi,

Remember to take care about different time between the Mysql server and the Apache/PHP server (if it's not the same computer). Because if you insert php date and you select now() mysql date, sometime you can have some vicious errors. (I've experienced this kind of problem when your Mysql server is set to summer time and your apache server is not : 2 different computers).

Regards.
#9

[eluser]riddla[/eluser]
I think you cold enable the Date Helper with
Code:
$this->load->helper('date');

and the use the following code also:
Code:
'last_edit_time' => trim(now());

Edit: I was thinking about Unix timestamps. The code above doesn't work for MySQL timesptamps. My bad.




Theme © iAndrew 2016 - Forum software by © MyBB