Problems with php echo umlauts - php

I want to echo some informations out of my datatable. Now i have the problem, that every column, where i´m using umlauts (like ä,ö) doesn´t appear. The whole content of this column doesn´t appear not just the umlauts.
In mysql im using the latin1_swedish_ci.
Any ideas?

Try to use UTF-8 (just run this query before select something from the DB)
<?php
...
mysql_query("SET NAMES 'utf8'");
...
?>

Related

Laravel 5.8 Trying to query Arabic records from the database

I'm trying to query some records in Arabic that I inserted earlier with the DB Facade with PDO. When I inserted those records the first time I noticed that everything inserted successfully but I can't see the Arabic text coming from the blade view file. So I checked the code and it was fine then I checked the database itself (Using TablePlus) and surprisingly found the Arabic text inserted successfully as well.
Next, I went to check my database charset located in Config/Database.php and found it's set to utf8mb4 and the collation is utf8mb4_unicode_ci, So this means that Arabic shouldn't be an issue (I even tried to switch it to only utf8 but no luck).
I went to check the query itself and I found that nothing is wrong, but as a courtesy, I thought I add "SET NAMES UTF8" before I execute my insert query and here is the weirdest thing... It showed the Arabic text when I redirected to the view after insertion. So I thought I only need to put "SET NAMES UTF8" before I select but when I did, it didn't show the Arabic text when I refreshed the view.
Files:
1 Controller:
It has 2 actions, the first one is to query and pass the result to view and the other one is to insert then execute the same query to redirect to the view with the result.
1 Blade View:
Has a form to import the file that has the data and a table to show the result. The same blade view I use just to show the result if there are new files to insert.
CODES:
Code to insert data:
DB::table('workers')->insert($data);
$pdo = DB::getPdo();
$statement = $pdo->prepare("set names utf8");
$statement = $pdo->prepare('select * from workers');
$statement->setFetchMode(\PDO::FETCH_ASSOC);
$statement->execute();
$results = $statement->fetchAll();
return view('payslips')->with('data', $results);
After executing this code I did dd($results) and I can see all data including Arabic text as well as the view shows everything.
When I refresh the page which executes the following code:
$pdo = DB::getPdo();
$statement = $pdo->prepare("set names utf8");
$statement = $pdo->prepare('select * from workers');
$statement->setFetchMode(\PDO::FETCH_ASSOC);
$statement->execute();
$results = $statement->fetchAll();
return view('payslips')->with('data', $results);
The Arabic text disappears again. Even when I dd($results) I can't even see the Arabic text in the query result.
In my blade view I use this format {{ $data['name'] }} to view records in table.
I tried the previously mentioned steps and also tried adding UTF8 in my HTML and everywhere to make sure it's not the problem but nothing changed. I tried rendering Arabic text statically and it showed no problems at all.
I also went to
/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php and injected the PDO initiation code with set names utf8 thinking it might fix the problem.
if (! empty($config['database'])) {
$connection->exec("use `{$config['database']}`;");
$connection->exec("set names utf8");
}
To me, it seems like there is something I should do with the database query or configuration or something but I really don't know what is it. It's a very weird problem for me and I really need your professional solution guys, please.
the best encoding to use for wide language support use
character set = utf8mb4
character collation = utf8mb4_unicode_ci
and for windows arabic:
cp1256 (Windows Arabic) collations:
cp1256_bin
cp1256_general_ci (default)
https://dev.mysql.com/doc/refman/8.0/en/charset-se-me-sets.html
Ok so the best way to show any characters other than English in blade is to use this format
{!! $data['name'] !!}
Also after removing all the UTF8 modifications I made, everything worked, So in my opinion, If you are working with Laravel 5.* and Arabic language... Don't panic! It's already dealing perfectly with Arabic.
Thank you all for your answers.

Cannot display special characters from mysql database using php

I apologize if this is a duplicate question. I am a newbie to special characters and know very little about special character encoding and decoding. I looked all over stackoverflow, tried the solutions but still cannot solve my problem. I have stored special characters in my mysql database, like prēməˈCHo͝or/,/-ˈt(y)o͝or. Database collation is utf8_bin. It shows fine in my database field but on my page, it displays as ?pr?m??CHo?or/,/-?t(y)o?or. I have added the following line to display this field:
<?php
echo strip_tags(html_entity_decode($row->Phonetic_eng,ENT_QUOTES, "UTF-8"));
?>
I have also added this to the php file:
<html><head><meta http-equiv="Content-type" content="text/html;charset=UTF-8"></head></html>
Kindly note that saving to database is not the issue I think. It is displaying the data that is the problem I think. In that case, how should I write the display statement:
<?php
echo strip_tags(html_entity_decode($row->Phonetic_eng,ENT_QUOTES, "UTF-8"));
?>
I would really appreciate any help. Thank you in advance
Assuming you're using MySQLi and something to the affect of:
$con=mysqli_connect("xxx","xxx","xxx","xxx");
use $con->set_charset("utf8"); just above your queries.
After selecting database, add this:
mysql_query("set names 'utf8'");

Retrieving chinese characters in database

i have a database that contains both chinese characters and english. when i queried the database to display them on my browser using php, all i get for the chinese characters are gibberish, none readable characters.
what i have tried:
ini_set('mssql.charset', 'UTF-8')
on the client side i made sure i included the meta tag specifying UTF-8.
am using mssql server
language is php
any help will be great. thanks
I worked on a Chinese migration project last year and can point you at a few things to look at:
Check you are using NVARCHAR and NCHAR as your DB data types. Do not use VARCHAR/CHAR
I've not used PHP much, but in the majority of languages I've worked in, you need to make sure the appropriate locale is set/supported.
So a few things to check, try outputing some Chinese text on a sample .php file to confirm it is displaying as expected. Next, update your schema with the correct data types. Finally, point your test script at the DB to see if the data is displayed as expected.
Try setting the character set within the PHP file after you've connected. Then run the query.
<?php
include('db_access.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
mysqli_set_charset($dbc, "utf8"); // Set the character set here!!
$query = "SELECT chinese FROM my_table";
while ($row = mysqli_fetch_array($query)) {
echo $row['chinese'].'<br />';
}
?>
PHP Manual: http://cn2.php.net/manual/zh/mysqli.set-charset.php

Diacritics from a mysql database not displayed correctly with an HTML and PHP page

I have a mysql database (Wamp) which uses latin1_swedish to encode characters (é, è, ...) but for an unknown reason when I display some results from the database (using an HTML page and a PHP page) the diacritics (à, é, è) are not displayed correctly.
I've already tried to change the charset of my webpage (iso 88599-1, utf-8) but it doesn't work.
echo htmlentities($variable_name);
Try to check the encoding on your files (you can do this with notapad++), in your database (you can see this in phpmyadmin), and your database connection (you can check this in your php file that connects to the db).
They should all match
If you know the character encoding that comes in and you know what goes out, you may be able to use iconv: http://www.php.net/manual/en/function.iconv.php
For example if the db is iso-8859 and you need utf-8, you call
iconv("ISO-8859-1", "UTF-8", $text);
You can't just change the character set of a table once values are stored, because the data will not be converted. You will have to convert the data using the available MySQL functions. Read more about that at http://dev.mysql.com/doc/refman/5.0/en/charset-convert.html.
Did you set your connection to mysql db with the
SET NAMES utf8;
command?
There are several ways:
mysql_query("SET NAMES 'utf8'");
and
mysql_query("SET CHARACTER SET utf8 ");
Or, the "best";
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);

How to encode cyrillic in mysql?

what's up? :-)
I have one problem and i hope you can help me with it.
One friend of mine have a simple solid html website and i implemented little php; CRUD system for articles... problem i came across is placing and getting cyrillic characters from mysql database.
What i want to achive is next:
In the main navigation there are some separated sections, whose names, ids and item's order i want to place in mysql and than to pull names and to put each name as a link. Names are supposed to be cyrillic characters.
The problem comes when i, using php mysql_fetch_assoc function, try to display names which are inserted with cyrillic characters in database row, collation of row is utf8_general_ci, and i end with ????? insted of original characters. If i submit cyrillic characters via submit form to mysql it shows something like this У.
How can i solve this, thanks in advance!? :-)
Make sure you call this after connecting to database.
mysql_query("SET NAMES UTF8");
Also make sure that HTML file has charset meta tag set to UTF-8 or send header before output.
header("Content-Type: text/html; charset=utf-8");
I had the same problem until I encoded the 'Collation' column in my table to 'utf8_bin'.
if its really mysql fetch assoc messing up you should try:
mysql-set-charset
from the docs:
Note:
This is the preferred way to change
the charset. Using mysql_query() to
execute SET NAMES .. is not
recommended.
also make sure your files are saved as utf8 and check iconv_set_encoding / iconv_get_encoding
For anyone having more complex issues with legacy project upgrades from versions before PHP 5.6 and MYSQL 5.1 to PHP 7 & Latest MySQL/Percona/MariaDB etc...
If the project uses utf8_encode($value) you can either try removing the function from the value being prepared and use the accepted answer for setting UTF-8 encoding for all input.
--- OR ---
Try replacing utf8_encode($value) with mb_convert_encoding($value, 'utf-8')
PDO USERS
If you are using PDO here are two ways how to set utf8:
$options = [
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
];
new \PDO($dsn, $username, $passwd, $options);
--- OR ---
$dsn = 'mysql:host=localhost;charset=utf8;'
new \PDO($dsn, $username, $passwd);
I can confirm that mb_convert_encoding($value, 'utf-8') to SQL table using utf8_unicode_ci works for Cyrillic and Umlaut.

Categories