1

I am using Codeigniter 2.1 and Microsoft Sql Server. When I try to insert UTF8 strings in database I get ??? characters. To connect to database I use SqlSrv driver.

2 Answers 2

2

Small change in SqlSrv driver solves the problem with unicode. Just add _make_unicode function code to /system/database/sqlsrv/sqlsrv_driver.php and replace the code for _insert and _update functions

/**
 * @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);
}

// --------------------------------------------------------------------
Sign up to request clarification or add additional context in comments.

Comments

1

Adding to Alex's answer (sorry not enough reputation to comment). Here are the same functions for 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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.