This question already has answers here:
UTF-8 all the way through
(13 answers)
Non English characters appear as question marks on my php page - appear fine in database
(2 answers)
Closed 7 years ago.
I have saved my data on MYSQL database using "utf8_general_ci". I wrote it on Bangla font.
But when I am trying to show data using php it is showing "????..." instead of showing the charecters.
<?php
$con = mysqli_connect("localhost","appsaadr_edu","pass","appsaadr_edu");
$result = mysqli_query($con,"SELECT * FROM lnews ORDER BY id DESC");
echo "{\"news\":";
$arr = array();
while($rows = mysqli_fetch_array($result)){
$test['id'] = $rows['id'];
$test['title'] = $rows['title'];
$test['text'] = $rows['text'];
$test['time'] = $rows['time'];
array_push($arr,$test);
}
echo json_encode($arr);
echo "}";
?>
What to do now?
Make sure the file is encoded in UTF-8 without BOM
Add : mysqli_set_charset($con,"utf8"); between the lines 3 and 5
Put all the $test['XXX'] like this utf8_encode($test['XXX'])
Make sure to add the data into the database also through utf8_encode before
Related
This question already has answers here:
check if url exists in php [duplicate]
(7 answers)
Closed 3 years ago.
I am displaying contents from the text file from database through file_get_contents.
$sql = "SELECT * FROM myContent";
$query = mysqli_query($db, $sql);
$row = mysqli_fetch_row($query);
$file = file_get_contents('mytext.txt');
echo $file;
Everything fine, but when my file is missing I get errors. How I put if-else condition in my php code when file is missed or not in place.
Thank you.
if(file_exists('mytext.txt')) {
$file = file_get_contents('mytext.txt');
echo $file;
}
Hope this helps ;)
if(!empty(file_get_contents('mytext.txt'))):
// Display file contents
else:
// Error handling
endif;
This question already has answers here:
PHP display image BLOB from MySQL [duplicate]
(2 answers)
How to retrieve images from MySQL database and display in an html tag
(4 answers)
Closed 4 years ago.
So, I want to make search results with image in PHP. For example, when you search for a product on a website, you get the search results with images, like this:
So I've created a database with one record only (type: BLOB):
Also, I have a PHP code for display images:
<?php
$server = mysqli_connect("localhost","root","") or die("Não consigo ligar à BD");
mysqli_select_db($server,"pesquisa") or die("Não encontro a BD");
$query = mysqli_query($server, "SELECT * FROM produtos");
while($row = mysqli_fetch_array($query)) {
echo '<img height="300" width="300" src="data:image;base64, '.$row[2].' ">';
}
mysqli_close($server);
?>
But when I run it on a browser, the result is this:
Is this code wrong?
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 6 years ago.
Hey so i'm getting text with latin characters like "ó" from a mysql database. This is the connection along with the query i'm using to get the data, placing it in an array:
<?php
header('Content-Type: text/html; charset=UTF-8');
$con=new mysqli ("localhost","root","","chroniclemark");
$sqlget = "SELECT descricao FROM items;";
$sqldata = mysqli_query($con,$sqlget) or die ('error getting database');
while ($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) {
$lol5[] = $row['descricao'] ;
}?>
I'm then trying to display the the text outside of the <?php ?> statement like so:
<p ><?php echo $lol5[0]; ?></p>
All of the text is fine except for the "ç", "ó", "ã" and so on.
I also have a meta tag <meta charset="UTF-8"> in my header but it doen't help.
In the database, the data is set to "utf8mb4_unicode_ci".
What can i do to fix this? Any idea?
#Fred -ii Thanks for pointing me in the right direction in the comments.
All i had to do was use mysqli_set_charset($con, 'utf8mb4'); before making the query
$sqlget = "SELECT descricao FROM items;";
$sqldata = mysqli_query($con,$sqlget) or die ('error getting database');
It's all fine now :)
This question already has answers here:
php json_encode not working on arrays partially
(2 answers)
Closed 6 years ago.
When I try to encode on a JSON a query from MySQL database that gives me a null JSON when the words have accents eg. " olá " , " à " etc
my php code:
$sql = "SELECT `name`
FROM `login`
WHERE `id`='1';";
$result = mysqli_query($conn, $sql);
$emparray = array();
while($row = mysqli_fetch_assoc($result)){
$emparray[] = $row;
}
echo json_encode($emparray,JSON_UNESCAPED_UNICODE);
my database:
id | name |
1 | Olá |
json_encode assumes that the result is encoded in UTF-8, if it is not returned null
first verify that your database server is configured with UTF_8
or add the set_charset before the consultation
mysqli_set_charset($conn,"utf8");
Use header to modify the HTTP header, UTF-8 all the way through
header('Content-Type: text/html; charset=utf-8');
Its working fine check Click Here
<?php
echo json_encode("Olá ",JSON_UNESCAPED_UNICODE);
?>
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 8 years ago.
I'm trying to print values from the database, containing countrylist. One of the fields in this database is cyrilic. My PHP script looks like this:
include_once("db.php");
$query = mysql_query ("SELECT * FROM country");
$row = mysql_fetch_array($query);
do {
$id = strtolower($row["id"]);
$value = ($row["runame"]);
echo $id . " " . $value . '<br/>';
} while ($row = mysql_fetch_array ($query));
The output from this script looks like this:
ad ???????
ae ???????????? ???????? ???????
af ??????????
ag ??????? ? ???????
ai ????????
So, apparently I need to set UTF-8 for this PHP script... somehow... Does anyone know how to do this?
Thank you for your time!
Try using SET NAMES utf8 after connecting to MySQL:
mysql_query("SET NAMES utf8");
Or if you are using cp1251 in the database, set this encoding.
As the manual says:
SET NAMES indicates what character set the client will use to send SQL
statements to the server... It also specifies the character set that the server should
use for sending results back to the client.