Welcome Guest, Not a member yet? Register   Sign In
CI with adodb and UTF8
#1

[eluser]hugle[/eluser]
Hello all.

I'm currently using adodb (lite) to connect to database.

The problem I have is that connections with MySQL server aren't in UTF8 charset (I believe so).

for example, when I try to get data from MySQL, I get weird characters.

The sollution is to add:

Code:
$this->adodb->query('SET character_set_database=UTF8');
$this->adodb->query('SET character_set_client=UTF8');
$this->adodb->query('SET character_set_connection=UTF8');
$this->adodb->query('SET character_set_results=UTF8');
$this->adodb->query('SET character_set_server=UTF8');
$this->adodb->query('SET names UTF8');

before connecting to MySQL all over the scripts.
the problem with that I need to add this in every script where I am using adodb.


So I thought I would need to put the code below somewhere in the library.
At the moment I have a library, called init_adodb.php

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class init_adodb
{
  function init_adodb()
  {
    if ( ! class_exists('ADONewConnection') )
    {
      require_once(APPPATH.'libraries/adodb/adodb.inc'.EXT);
      require_once(APPPATH.'libraries/adodb/adodb-error.inc'.EXT);
    }

    $this->obj =& get_instance();
    $this->_init_adodb_library();
  }

    function _init_adodb_library()
    {
        $db_var = false;
        $debug = false;
        $show_errors = true;
        $active_record = false;

        // try to load config/adodb.php
        // CI 1.4.0+
        if ($this->obj->config->load('adodb',true,true)) {
            $cfg = $this->obj->config->item('adodb','adodb');
            if (isset($cfg['dsn'])) {
                $dsn = $cfg['dsn'];
            }

            if (isset($cfg['db_var'])) $db_var = $cfg['db_var'];
            if (isset($cfg['debug'])) $debug = $cfg['debug'];
            if (isset($cfg['show_errors'])) $show_errors = $cfg['show_errors'];
            if (isset($cfg['active_record'])) $active_record = $cfg['active_record'];

        }

        if (!isset($dsn)) {
            // fallback to using the CI database file
            include(APPPATH.'config/database'.EXT);
            $group = 'default';
            $dsn = $db[$group]['dbdriver'].'://'.$db[$group]['username'].':'.$db[$group]['password'].'@'.$db[$group]['hostname'].'/'.$db[$group]['database'];
        }

        if ($show_errors) {
            require_once(APPPATH.'libraries/adodb/adodb-errorhandler.inc'.EXT);
        }

        // $this->obj is by reference, refers back to global instance
        $this->obj->adodb =& ADONewConnection($dsn);
        //$this->obj->adodb->setFetchMode(ADODB_FETCH_ASSOC);

        if ($db_var) {
            // also set the normal CI db variable
            $this->obj->db =& $this->obj->adodb;
        }

        if ($debug) {
            $this->obj->adodb->debug = true;
        }

        if ($active_record) {
            require_once(APPPATH.'libraries/adodb/adodb-active-record.inc'.EXT);
            ADOdb_Active_Record::SetDatabaseAdapter($this->obj->adodb);
        }
    }

  /**     "Factory" function to get an ADOdb ActiveRecord object for a certain table name
   * Eg:
   *   $person =& ADOdb_Active_Record_Factory('person');
   * creates a class called "person" that extends ADOdb_active_record and returns an instance of it.
   * The class will use the ADOdb table-naming conventions, so the active record will use the 'persons' table
   * To override the table name:
   *   $person =& ADOdb_Active_Record_Factory('person', 'people');
   */
  function ADOdb_Active_Record_Factory($classname, $tablename = null)
  {
    // create the class
    eval('class '.$classname.' extends ADOdb_Active_Record{}');

    if ($tablename != null) {
      return new $classname($tablename);
    } else {
      return new $classname;
    }
  }
}

/* End of file init_adodb.php */
/* Location: ./system/application/libraries/init_adodb.php */

?>

Maybe I should Insert those mysql command somewhere here to make it something like GLOBAL over all adodb <> mysql connections ?

Thank You all
CI is great!

cheers,
Jaroslav




Theme © iAndrew 2016 - Forum software by © MyBB