2014-04-10 2 views
1

Codeigniter 2.1 및 Microsoft SQL Server를 사용하고 있습니다. 내가 데이터베이스에 UTF8 문자열을 삽입하려고하면 ??? 문자. 데이터베이스에 연결하려면 SqlSrv 드라이버를 사용하십시오.Insert-update utf8 string sql server + codeigniter

답변

1

SqlSrv 드라이버가 약간 변경되면 유니 코드 문제가 해결됩니다. 그냥 /system/database/sqlsrv/sqlsrv_driver.php 및 _insert 및 _update 기능 알렉스의 대답에 추가

/** 
* @param mixed $value 
*/ 
function _make_unicode($value){ 
    if(is_string($value) && $value!="NULL")return "N".$value; 
    else return $value; 
} 

/** 
* Insert statement 
* 
* Generates a platform-specific insert string from the supplied data 
* 
* @access public 
* @param string the table name 
* @param array the insert keys 
* @param array the insert values 
* @return string 
*/ 
function _insert($table, $keys, $values) 
{ 
    $values_string = ""; 
    for($i=0;$i<count($values);$i++){ 
     if($i==0){ 
      $values_string.= $this->_make_unicode($values[$i]); 
     }else{ 
      $values_string.= ", ".$this->_make_unicode($values[$i]); 
     } 
    } 
    return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES ({$values_string})"; 
} 

// -------------------------------------------------------------------- 

/** 
* Update statement 
* 
* Generates a platform-specific update string from the supplied data 
* 
* @access public 
* @param string the table name 
* @param array the update data 
* @param array the where clause 
* @param array the orderby clause 
* @param array the limit clause 
* @return string 
*/ 
function _update($table, $values, $where) 
{ 
    foreach($values as $key => $val) 
    { 
     $valstr[] = $key." = ".$this->_make_unicode($val); 
    } 

    return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where); 
} 

// -------------------------------------------------------------------- 
1

(죄송 충분하지 명성 의견을)에 대한 코드를 대체 할 _make_unicode 기능 코드를 추가합니다. CodeIgniter 3.1.1과 동일한 기능이 있습니다.

_insert_batch

/** 
* Insert batch statement 
* 
* Generates a platform-specific insert string from the supplied data. 
* 
* @param string $table Table name 
* @param array $keys INSERT keys 
* @param array $values INSERT values 
* @return string|bool 
*/ 
protected function _insert_batch($table, $keys, $values) 
{ 
    // Multiple-value inserts are only supported as of SQL Server 2008 
    if (version_compare($this->version(), '10', '>=')) 
    { 
     foreach($values as &$value) { 
      $value = $this->_make_unicode($value); 
     } 

     return parent::_insert_batch($table, $keys, $values); 
    } 

    return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; 
} 

_update

/** 
* Update statement 
* 
* Generates a platform-specific update string from the supplied data 
* 
* @param string $table 
* @param array $values 
* @return string 
*/ 
protected function _update($table, $values) 
{ 
    foreach ($values as &$value) { 
     $value = $this->_make_unicode($value); 
    } 


    $this->qb_limit = FALSE; 
    $this->qb_orderby = array(); 
    return parent::_update($table, $values); 
}