Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading UTF-8 data from MySQL shows ? instead of ı

Tags:

php

mysql

utf-8

Here is how I read the data:

<?php
$id = $_GET["id"];
$number = mysql_real_escape_string($id);
$result = mysql_query('SELECT * FROM `mystory` where `id` = ' . "$number" . ' LIMIT 1');

$row = mysql_fetch_assoc($result);
echo $row['story'];
?>

The data is encoded as utf8_bin. Insted of ı PHP outputs ?

Why is that?

like image 249
ilhan Avatar asked Dec 08 '22 02:12

ilhan


2 Answers

You need to make sure that your connection to the database us in UTF-8 as well. You can do that this way:

mysql_query("SET NAMES utf8");

Also make sure that you are telling the browser that it is UTF-8:

header("Content-type: text/html; charset=utf-8");
like image 175
Daniel Egeberg Avatar answered Dec 10 '22 11:12

Daniel Egeberg


This is the only code worked for me :-

mysqli_set_charset($con,'utf8');

Where $con is your connection value to DB . Be sure put the code after connect and error code and you will be fine .

like image 25
Salem Avatar answered Dec 10 '22 13:12

Salem