我正在尝试将土耳其字符改为英文字符,但它没有改变。我想 在mysql数据库上运行查询.Mysql不接受查询的土耳其语字。所以我无法运行查询。
我的SQL查询是:
$sql_test="SELECT a.order_id as siparis_no,
a.firstname as isim,
a.lastname as soyisim,
b.name as urun_Adi
FROM zisl8_mijoshop_order a
LEFT JOIN zisl8_mijoshop_order_product b
ON a.order_id=b.order_id
WHERE a.date_added>'$t_bas' and a.date_added < '$t_bit' AND b.name='Genel Bağış'
";
我的PHP替换代码:
$replace = array('i', 's', 'o', 'c', 'g', 'u', 'I', 'G', 'O', 'C', 'S', 'U');
$search = array('ı', 'ş', 'ö', 'ç', 'ğ', 'ü', 'İ', 'Ğ', 'Ö', 'Ç', 'Ş', 'Ü');
$str='Genel Bağış';
$str=str_replace($search,$replace,$str);
我知道PHP代码没有问题。但是当试图在页面上显示echo:
字符串显示如此'GenelBağış'。所以似乎没有我想做的改变。
并且MySQL查询不起作用。但是当我像这样手动更改字符串时:
$sql_test="SELECT a.order_id as siparis_no,
a.firstname as isim,
a.lastname as soyisim,
b.name as urun_Adi
FROM zisl8_mijoshop_order a
LEFT JOIN zisl8_mijoshop_order_product b
ON a.order_id=b.order_id
WHERE a.date_added>'$t_bas' and a.date_added < '$t_bit' AND b.name='Genel Bagis'
";
查询正在工作。'GenelBağış'字符串来自mysql db。这是为了说明我的问题。
那我在哪里做错了,请帮忙吗?
答案 0 :(得分:0)
在每个PHP输出标头中,指定UTF-8作为编码:
header('Content-Type: text/html; charset=utf-8');
<强>的MySQL _ * 强>
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8', $link);
不推荐使用mysql_ *
<强>的mysqli 强>
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
$mysqli->set_charset("utf8");
<强> PDO 强>
$pdo = new PDO(
'mysql:host=mysql.example.com;dbname=example_db',
"username",
"password",
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
在MySQL方面,需要对my.ini
文件进行如下修改:
[client]
default-character-set=UTF-8
[mysql]
default-character-set=UTF-8
[mysqld]
character-set-client-handshake = false #force encoding to uft8
character-set-server=UTF-8
collation-server=UTF-8_general_ci
[mysqld_safe]
default-character-set=UTF-8