插入更新utf8字符串sql server + codeigniter

时间:2014-04-10 07:55:34

标签: sql-server codeigniter utf-8

我正在使用Codeigniter 2.1和Microsoft Sql Server。当我尝试在数据库中插入UTF8字符串时,我得到???字符。要连接到数据库,我使用SqlSrv驱动程序。

2 个答案:

答案 0 :(得分:1)

SqlSrv驱动程序中的小变化解决了unicode的问题。 只需将_make_unicode函数代码添加到/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 :(得分:1)

添加到Alex的答案(抱歉没有足够的声誉来评论)。以下是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);
}