开发者

MySQL db question marks instead of hebrew characters..?

开发者 https://www.devze.com 2023-02-16 17:23 出处:网络
I\'m trying to build a shopping cart using PHP & MySQL. my db in MySQL 开发者_运维百科is utf8 and my table in the db is utf8,

I'm trying to build a shopping cart using PHP & MySQL. my db in MySQL 开发者_运维百科is utf8 and my table in the db is utf8,

How can I use Hebrew characters?


I was able to solve this by doing the following:

  1. the db collation has to be utf8_general_ci
  2. the collation of the table with hebrew has to be utf8_general_ci
  3. in your php connection script put header('Content-Type: text/html; charset=utf-8');
  4. in xhtml head tag put <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. after selecting the db in the connection script put mysql_query("SET NAMES 'utf8'");


After a lot of work I found a solution that always works..: without SET_NAMES.

In the conn.inc.php file, after you selected a database and connected to it, do this:

if(!mysqli_set_charset($conn, 'utf8')) {
    echo 'the connection is not in utf8';
    exit();
}

...and in the html always use charset utf-8;

That solved it for me. No need to use set_names(), which is ok but it annoyed the hell out of me.


You can use the PDO in your code like this:

 $db = new PDO($config['DSN'], $config['dbUserName'], $config['dbPassword']);
 $db->exec("SET NAMES 'utf8'");

Make sure you include the single quotation marks around the 'utf8'.


If it's an encoding problem (and it sounds like it is), this query will help:

SET NAMES utf8

Execute this query (e.g. mysql_query("SET NAMES utf8")) right after you connect and before you move any data.

More info


$conn->set_charset("utf8"); Use this for your dbconnect


Where are the question marks showing up? It may be an encoding problem somewhere other than in the data base.


To store non-ASCII characters in a database column, you need to define that column with a specific character set. You can specify a character set at 3 levels: database, table, and column. For example:

CREATE DATABASE db_name CHARACTER SET utf8
CREATE TABLE tbl_name (...) CHARACTER SET utf8
CREATE TABLE tbl_name (col_name CHAR(80) CHARACTER SET utf8, ...)

http://www.herongyang.com/PHP/Non-ASCII-MySQL-Store-Non-ASCII-Character-in-Database.html


In my case I created the DB from a dump, and this command solve it:

ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;


mysql_query('SET NAMES utf8') ..... Put this in Php

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号