简体   繁体   English

SQL NOW()给出了00:00:00

[英]SQL NOW() gives 0000-00-00 00:00:00

i have done SQL insertion with NOW() using codeigniter. 我使用codeigniter使用NOW()完成了SQL插入。 instead of current time i got 0000-00-00 00:00:00 in my db recode. 而不是当前时间我在我的数据库重新编码中得到0000-00-00 00:00:00。 i am using latest WAMP as my server and cannot understand the where is the problem. 我使用最新的WAMP作为我的服务器,无法理解问题出在哪里。 please help me. 请帮我。

function createform($form_data)
{
    $this->db->set('created', 'NOW()',TRUE);
    $this->db->insert('amfbases', $form_data);

    if ($this->db->affected_rows() == '1')
    {
        return TRUE;
    }

    return FALSE;
}

check your column data type, and did u quote the NOW(), like insert into table values ("NOW()"); 检查您的列数据类型,并引用NOW(),如insert into table values ("NOW()"); ?

updated 更新

$this->db->set('created', time(),TRUE);

or 要么

$this->db->set('created', date('Y-m-d H:i:s'),TRUE);

or update your table schema to add_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP 或者将表模式更新为add_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP

updated 2 更新2

NOW() is specify to MYSQL, PHP interpreted it as string, and cannot mix both together NOW()指定给MYSQL,PHP将其解释为字符串,并且不能将它们混合在一起

Actually the right way to do this (ie pass sql functions to db) is to specify third param to be FALSE Example: 实际上,正确的方法(即将sql函数传递给db)是指定第三个参数为FALSE示例:

$this->db->set('created', 'NOW()', FALSE); // notice FALSE

That will cause codeigniter to pass second param without quoting it resulting in sql interpreting it as function. 这将导致codeigniter传递第二个参数而不引用它导致sql将其解释为函数。

Useful for: 对...有用:

$this->db->set('counter', 'counter + 1', FALSE);

For reference look: http://codeigniter.com/user_guide/database/active_record.html 有关参考,请参阅: http//codeigniter.com/user_guide/database/active_record.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM