Why does the replacement character keeps appearing when echoing variable? - php

I'm creating a test application and keeping the name of the person who is taking it on a table, along with the answers. The problem lies when I enter the following characters
á é í ó ú ñ
I've tried some methods (like iconv function) but still not working.
//A sample of the code
require('../connect_db.php');
setlocale(LC_ALL,"es_ES");
mysql_query("SET NAMES 'utf8'");
$sql = "SELECT *
FROM database.test";
$result = mysql_query($sql) or die (mysql_error());
$row = mysql_fetch_array($result);
$name = utf8decode($row[1]);
echo mb_detect_encoding($name); //Just to test
echo $name;
The result:
UTF-8 G�mez
Edit: The problem is not on the database. I've manually entered names with these characters with no problem, so it must be the code.

SOLVED
There was no need to run the first utf8decode() when assigning values to $name
Instead I just called the function at the echo

Related

Select hebrew from mysql database

honestly I am coming here after trying everything I could find online in order to fix that problem but nothing worked..
I have a phpmyadmin database table that encoded to utf_general_ci, when I insert data in Hebrew into it it works fine and I can see the Hebrew in the table on PhpMyAdmin, but when I need to retrieve the Hebrew data from the table that's when the problem starts...
right now I am using a simple sql query to select data from the table and I tried every code that should fix it I could find online but nothing seems to work.
my current code is:
<?php
$db = "android";
$username = "root";
$password = "";
$host = "localhost";
$sql = "select * from sample;";
$conn = mysqli_connect($host,$username,$password,$db);
$conn->set_charset('utf8');
$result = mysqli_query($conn,$sql);
$response = array();
while($row = mysqli_fetch_array($result))
{
array_push($response,array($row[0],$row[1]));
}
$str = json_encode(array($response));
echo $str;
mysqli_close($conn);
?>
and the output of the Hebrew word is \u05d4\u05d9\u05d9 what seems like a different encoding, my php file is encoded to UTF-8 and I am using npp to write it.
please try helping me out on this one cause I couldn't find an answer
This is a json_encode() behaviour, by default Unicode char are escaped as \u...
Try with JSON_UNESCAPED_UNICODE flag :
<?php
echo json_encode($arr, JSON_UNESCAPED_UNICODE);

PHP: JSON output empty because of german char

There are a lot of topics who discuss my problem but I cant still solve it.
I have a database which I encode by JSON. But when I put content with german char like ä, ö, ü etc the output is empty.
Here is my php script:
<?PHP
error_reporting(E_ALL);
ini_set('display_errors', '1');
//Verbindung zur Datenbank
include "inc_mysql.php";
//SQL-String
$select = "SELECT * FROM module ORDER BY name ASC";
//SQL-Befehl in Variable speichern
$sql = mysql_query($select);
//********************this one was missing!********************
mysql_set_charset("utf8");
//Array erstellen
$jsonArray = array();
while ($row = mysql_fetch_array($sql)){
$jsonArray[] = $row;
}
echo json_encode($jsonArray, JSON_UNESCAPED_UNICODE);
?>
The php file is UTF-8 without BOM.
The column is the dabase is utf8_general_ci.
BTW: The error reporting just says something about mysql_query() is depricated.
Might be a easy game for you guys :)
Solution found! Check the code!
You can use json_last_error_msg() function to show the error message. http://php.net/manual/en/function.json-last-error-msg.php
This function mb_convert_encoding() may help you.

Store and output text with accents

I have some text in a database. I use French and English. French has accents, and some special characters like ç. I use Mamp, MySQL and PHP.
I have collation latin1_swedish-ci (the default). I tried utf8_general_ci and the result is the same.
If I use in a html page, I have this in the head: <meta charset="UTF-8">
As an example, in the database I have "voilà".
When I echo the text from the database to html:
$con = mysqli_connect("localhost","root","root");
if (!$con) {
die('The connexion failed: ' . mysqli_error());
}
if (!mysqli_select_db($con, 'prova')){
echo "Connection with database was not possible";
}
$result = mysqli_query($con, "SELECT * FROM test1
WHERE id='1' ")
or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
$text = $row['first'];
echo $text; //I see: voil�
echo htmlentities($text); //I see nothing
echo utf8_encode($text); //This works: I see voilà
}
Why htmlentities does not work?
Is utf8_encode(); the way to go? I have to use that always when I output something from the database? Why do I have to use that if the collation is already UTF8? Is there any better way to store and output text with accents in a MySQL database?
After you connect to the DB you should set the client charset to UTF8:
mysqli_set_charset($con, "UTF8");
Otherwise the mysql client transforms the UTF8 'voilà' to latin1 ('cause it seems that is it's default).
Either you tell the client that I want everything in UTF8, or you get it with the default latin1, and convert it one-by-one yourself calling utf8_encose($text)

Cyrillic displays as ??? ???? in PHP [duplicate]

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.

How to resolve mysql text encoding issue

I'm struggling with a mysql import issue and the usual remedies don't seem to be working. I'm trying to copy fields from one database to another (both Drupal systems).
Running "show table status on the databases" I notice that the origin table is utf8_bin and the destination table is utf8_general_ci.
I'm currently doing the import like this:
$olddb = mysql_connect("localhost", "user", "password");
mysql_select_db("origin", $olddb);
$result = mysql_query("set names utf8", $olddb);
$newdb = mysql_connect("localhost", "user", "password");
mysql_select_db("destination", $newdb);
$result = mysql_query("set names utf8", $newdb);
$result = mysql_query("select first_name from origin_table", $olddb);
$row = mysql_fetch_array($result);
$query = "update destination_table set first_name = \"".$row["first_name"]."\"";
$result = mysql_query($query, $olddb);
The text looks like its importing correctly but when I try and edit the same fields in Drupal, I get the following weird question-mark characters between every character.
Fields in Safari browser
Fields in Firefox browser
Any ideas?
How are you outputting the data from PHP->browser? You'd need to issue the appropriate headers to tell the browser to expect UTF-8 text, e.g:
header("Content-Type: text/plain; charset=utf-8");
Given the "weird" characters, I'm guessing FF is seeing the text as something non-unicode, like iso8859-1 and assuming 1 byte/character.

Categories