如何在codeigniter中将当前日期时间插入oracle数据库[重复]

问题描述 投票:0回答:4

我试图在 Codeigniter 中将当前日期时间插入到 Oracle 数据库中,但出现如下错误:

ORA-01858: a non-numeric character was found where a numeric was expected

INSERT INTO "USER_LOGS" ("USER_ID", "IP_ADDRESS", "LOGIN_DATE", "LOG_TYPE", "USERNAME") VALUES ('1', '::1', 'TO_DATE(''2018-07-17 03:33:03'',''yyyy/mm/dd hh24:mi:ss'')', 'LOG OUT', 'admin')

我的代码:

date_default_timezone_set("Asia/Dhaka");
$timestamp = date('Y-m-d h:i:s');
$timestamp = "TO_DATE('$timestamp','yyyy/mm/dd hh24:mi:ss')";
$user_log_data = array(
   'USER_ID' => $ses_data['USER_ID'],
   'IP_ADDRESS' => $this->input->ip_address(),
   'LOGIN_DATE' => $timestamp,
   'LOG_TYPE' => 'LOG OUT',
   'USERNAME' => $ses_data['USERNAME']
);
$this->utilities->insertData($user_log_data, 'USER_LOGS');

我的插入语句:

function insertData($post, $tableName)
    {
        $this->db->trans_start();
        $this->db->insert($tableName, $post);
        $this->db->trans_complete();
        if ($this->db->trans_status() == TRUE) {
            return TRUE;
        } else {
           return FALSE;
        }


    }

为了您的信息,字段(LOGIN_DATE)的数据类型是TIMESTAMP

谢谢

php oracle codeigniter datetime
4个回答
1
投票

您可以像这样以不同的方式构建 INSERT 语句:

$sql = 
   "INSERT INTO USER_LOGS (USER_ID, IP_ADDRESS, LOGIN_DATE, LOG_TYPE, USERNAME) 
    VALUES (?, ?, TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?)";

$timestamp = date('Y-m-d h:i:s');
$user_log_data = array(
   $ses_data['USER_ID'],
   $this->input->ip_address(),
   $timestamp,
   'LOG OUT',
   $ses_data['USERNAME']
);

$this->db->query($sql, user_log_data);

或者如果您询问“当前日期时间”,则只需:

$sql = 
   "INSERT INTO USER_LOGS (USER_ID, IP_ADDRESS, LOGIN_DATE, LOG_TYPE, USERNAME) 
    VALUES (?, ?, SYSDATE, ?, ?)";
$user_log_data = array(
   $ses_data['USER_ID'],
   $this->input->ip_address(),
   'LOG OUT',
   $ses_data['USERNAME']
);
$this->db->query($sql, user_log_data);

但是,我不知道

db->query()
是否也适用于DML语句。


1
投票

我的数据类型列表是 TIMESTAMP 然后我在我的控制器中使用这样的,它的工作原理:

public function __construct() {
 parent::__construct();    
 
public function update() {

$date = new DateTime();
$date->setTimezone(new DateTimeZone('Asia/Jakarta'));

$data = array( 
'UPDATED_AT' => $date->format('d-M-y h:i:s A'),
 );
 $this->kelola->update_piping(array('JOINT_NO' => $joint , 'DWG_PIPE_ID' 
  => $dwgPipeId), $data, $data1);
        
   echo json_encode(array("status" => TRUE));
    }

0
投票

请从 $timestamp 变量中删除单引号后尝试。

    date_default_timezone_set("Asia/Dhaka");
    $timestamp = date('Y-m-d h:i:s');
    $timestamp = "TO_DATE($timestamp,'yyyy/mm/dd hh24:mi:ss')";
    $user_log_data = array(
       'USER_ID' => $ses_data['USER_ID'],
       'IP_ADDRESS' => $this->input->ip_address(),
       'LOGIN_DATE' => $timestamp,
       'LOG_TYPE' => 'LOG OUT',
       'USERNAME' => $ses_data['USERNAME']
    );
    $this->utilities->insertData($user_log_data, 'USER_LOGS');

0
投票

最后,我通过使用 CI 日期助手并在 system/core/Controller.php 中创建自定义 Oracle 日期格式方法来完成此操作,或者您可以创建自己的继承 CI_Controller 的控制器

//system/core/Controller.php
class CI_Controller {
function __construct() {
        parent::__construct();            
    }

public function oracle_date($timestamp='')
    {
        $this->load->helper('date');
        if($timestamp=='date'){
            $datestring = '%d-%M-%Y';
        }
        else
        {
            $datestring = '%d-%M-%Y %h.%i.%s %a';
        }

        $time = time();
        $timestamp = strtoupper(mdate($datestring, $time));
        return $timestamp;
    }

 }

//OR

//application/core
class MY_Controller extends CI_Controller {
function __construct() {
        parent::__construct();            
    }

public function oracle_date($timestamp='')
    {
        $this->load->helper('date');
        if($timestamp=='date'){
            $datestring = '%d-%M-%Y';
        }
        else
        {
            $datestring = '%d-%M-%Y %h.%i.%s %a';
        }

        $time = time();
        $timestamp = strtoupper(mdate($datestring, $time));
        return $timestamp;
    }

 }

//Then use as follows:

//application/controllers
class User extends MY_Controller {
    public function __construct(){
          parent::__construct();
    }

   $timestamp = $this->oracle_date('timestamp'); //if parameter is 'date' it will return only date like DD-MM-YY and 'timestamp' it will return full timestamp

}

//OR

//application/controllers
class User extends CI_Controller {
    public function __construct(){
          parent::__construct();
    }

   $timestamp = $this->oracle_date('timestamp'); //if parameter is 'date' it will return only date like DD-MM-YY and 'timestamp' it will return full timestamp


}

如果您创建自定义控制器,您还可以在 config.php 文件中更改前缀 MY_,例如 $config['subclass_prefix'] = 'MY_';到 $config['subclass_prefix'] = 'Your_';

© www.soinside.com 2019 - 2024. All rights reserved.