MySQL db question marks instead of hebrew characte

2020-01-24 14:21发布

问题:

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?

回答1:

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'");


回答2:

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.



回答3:

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'.



回答4:

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



回答5:

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



回答6:

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



回答7:

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



回答8:

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;


回答9:

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