Giter Site home page Giter Site logo

UTF8 about sparrow HOT 4 OPEN

mikecao avatar mikecao commented on May 10, 2024
UTF8

from sparrow.

Comments (4)

mikecao avatar mikecao commented on May 10, 2024

You can declare your own PDO object with the correct charset and then pass it into sparrow.

See http://stackoverflow.com/questions/4361459/php-pdo-charset-set-names

from sparrow.

kamov avatar kamov commented on May 10, 2024

Thanks for your reply.
Yes I understand this.
However I am trying to allow my application to work with different database engine.

So I initizialize the database in this way:

public function __construct($config)
{
    $this->db = new Sparrow();
    $this->db->setDb($config);
}

where $config is:

[
'type' => 'mysqli', // mysqli | mysql | pgsql | sqlite | sqlite3 | pdomysql | pdopgsql | pdosqlite
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mydatabase'
'charset' => 'utf8'
]

So is only enough to change config in order to change database engine.

from sparrow.

kamov avatar kamov commented on May 10, 2024

For example this is for mysqli, mysql and pdo:

switch ($db['type']) {
                case 'mysqli':
                    $this->db = new mysqli(
                        $db['hostname'],
                        $db['username'],
                        $db['password'],
                        $db['database']
                    );

                    if ($this->db->connect_error) {
                        throw new Exception('Connection error: '.$this->db->connect_error);
                    }

                    /* Change character set */
                    if (! $this->db->set_charset($db['charset'])) {
                        throw new Exception("Error loading character set utf8: %s\n", $this->db->error);
                    }

                    break;

                case 'mysql':
                    $this->db = mysql_connect(
                        $db['hostname'],
                        $db['username'],
                        $db['password']
                    );

                    if (!$this->db) {
                        throw new Exception('Connection error: '.mysql_error());
                    }

                    /* Change character set */
                    mysql_set_charset($db['charset'], $this->db);

                    mysql_select_db($db['database'], $this->db);

                    break;

                case 'pdomysql':
                    $dsn = sprintf(
                        'mysql:host=%s;port=%d;dbname=%s;charset=%s',  /* Change character set */
                        $db['hostname'],
                        isset($db['port']) ? $db['port'] : 3306,
                        $db['database'],
                        $db['charset']  /* Change character set */
                    );

                    $this->db = new PDO($dsn, $db['username'], $db['password']);
                    $db['type'] = 'pdo';

                    break;

            }

from sparrow.

kamov avatar kamov commented on May 10, 2024

For pgsql, I think that is http://php.net/manual/en/function.pg-set-client-encoding.php
but I am not sure.

Not sure also for sqlite and sqlite3.

And in last, for pdomysql and pdosqlite, I think that only way is to run query:

$this->db->('SET NAMES UTF8');

I guess why not add this on sparrow?

thanks

from sparrow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.