Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix charset #27

Open
kamov opened this issue Nov 9, 2016 · 3 comments
Open

Fix charset #27

kamov opened this issue Nov 9, 2016 · 3 comments
Assignees

Comments

@kamov
Copy link

kamov commented Nov 9, 2016

Hi,
can you please explain how to fix charset for different case?
For example I am using mysql and sqlite, and also pdo in some project.
I try to check what other suggest, but without success.
Maybe you can make a method for handle this?
THanks!!

@kamov
Copy link
Author

kamov commented Nov 10, 2016

strange, I have posted here more reply.. seem disappear..

@kamov
Copy link
Author

kamov commented Nov 13, 2016

I will write again my previous posts, there was many more info, so I will make this time short.

I would like to fix charset for all type of database engine so my application can be database agnostic.

I have change the setDb() in this way below for mysql, mysqli and pdomysql. But I am not sure how to do for others apart run query SET NAMES UTF8.

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 'pgsql':
                    $str = sprintf(
                        'host=%s dbname=%s user=%s password=%s',
                        $db['hostname'],
                        $db['database'],
                        $db['username'],
                        $db['password']
                    );

                    $this->db = pg_connect($str);

                    // @TODO $this->db->("SET NAMES {$db['charset']}");

                    break;

                case 'sqlite':
                    $this->db = sqlite_open($db['database'], 0666, $error);

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

                    // @TODO $this->db->("SET NAMES {$db['charset']}");

                    break;

                case 'sqlite3':
                    $this->db = new SQLite3($db['database']);

                    break;

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

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

                    // @TODO $this->db->("SET NAMES {$db['charset']}");

                    break;

                case 'pdopgsql':
                    $dsn = sprintf(
                        'pgsql:host=%s;port=%d;dbname=%s;user=%s;password=%s',
                        $db['hostname'],
                        isset($db['port']) ? $db['port'] : 5432,
                        $db['database'],
                        $db['username'],
                        $db['password']
                    );

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

                    // @TODO $this->db->("SET NAMES {$db['charset']}");

                    break;

                case 'pdosqlite':
                    $this->db = new PDO('sqlite:/'.$db['database']);
                    $db['type'] = 'pdo';

                    // @TODO $this->db->("SET NAMES {$db['charset']}");

                    break;
            }

I hope you can integrate this in the library, so we can just run $db->setDb(array with all info charset included)

Thanks!

@mikecao mikecao self-assigned this Nov 13, 2016
@mikecao
Copy link
Owner

mikecao commented Nov 13, 2016

I think it should be possible for the databases that support charset. I'll work on an enhancement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants