How to import Romanian diacritics from csv to mysql database - php

i have found one error on my script.
While is importing from csv file to MySQL database diacritics shown strange char.
i need fast and realiable solution in SQL query in my code below:
$sql = "INSERT into medici (`cnp`, `cod_parafa`, `tit_id`, `numele`, `numele_anterior`,...
what i need is importing fields: numele and numele_anterior with support my romanian char like șțăîâ.
My database has set utf8_romanian_ci char.

Related

PhpStorm Import data from CSV file to MySQL, Cyrillic problem

I have problem with encoding while trying to import data from CSV file to MySQL database in PhpStorm. The CSV file contains Cyrillic symbols. I've read some articles, tried many things, but none of them worked for me.
In my.ini file I changed:
collation_server=utf8_unicode_ci to collation_server=utf8mb4_unicode_ci and
character_set_server=utf8 to character_set_server=utf8mb4
but the result was still the same.
That's my CSV file:
NAME_1,NAME_2,NAME_3
Vladimir,Ivanov,Kaziyski
ИВАН,ПЕТРОВ,ГЕОРГИЕВ
John,Lee,Smith
ПЕТЪР,ЙОРДАНОВ,ПЕТРОВ
In PhpStorm I click right-button on the database which I will import data and then I click:
then I chose the CSV file
and in the Data Preview section everything looks normal, encoding is set to UTF-8
and after that I get this error
Here is the log:
2:1: Data truncation: Incorrect string value: '\xD0\x98\xD0\x92\xD0\x90...' for column `current_db`.`csv_data`.`NAME_1` at row 2
3:1: Data truncation: Incorrect string value: '\xD0\x98\xD0\x92\xD0\x90...' for column `current_db`.`csv_data`.`NAME_1` at row 1
4:1: Data truncation: Incorrect string value: '\xD0\x98\xD0\x92\xD0\x90...' for column `current_db`.`csv_data`.`NAME_1` at row 2
5:1: Data truncation: Incorrect string value: '\xD0\x9F\xD0\x95\xD0\xA2...' for column `current_db`.`csv_data`.`NAME_1` at row 1
I've searched this error and tried almost all solutions, but nothing worked. The problem for sure is Cyrillic. Does anyone with PhpStorm had the same issue and how to fix it?
utf8 vs utf8mb4 does not matter for Cyrillic, only for Emoji and Chinese.
Please provide SHOW CREATE TABLE. I suspect you did not change the CHARACTER SET for the column(s) that will receive the Cyrillic characters.

How to trim leading spaces while importing Excel into mysql

i am importing a CSV file into mysql database. One whole column (Project_ID_) in the excel sheet has leading space in it and after importing, the space is converted into garbage value in mysql table.
For example:
Value in Excel is : 000000000015382
Value in Mysql becomes: � 000000000015382
how do i trim the space
code for importing is given below:
$import="INSERT INTO streammapping(Project_ID_Numeric
,Project_ID_
,Project_Description_
,Action
,Transition_Steady_State
,Stream
,Stream_Lead
,Fixed_Variable
,Domain
,Pillar
,Account_Plan_Pilar
,Account_Plan_Project_Type)
VALUES('".mysql_real_escape_string($data[0])."','".mysql_real_escape_string($daa[1])."','".mysql_real_escape_string($data[2])."','".mysql_real_escape_string($data[3])."','".mysql_real_escape_string($data[4])."','".mysql_real_escape_string($data[5])."','".mysql_real_escape_string($data[6])."','".mysql_real_escape_string($data[7])."','".mysql_real_escape_string($data[8])."','".mysql_real_escape_string($data[9])."','".mysql_real_escape_string($data[10])."','".mysql_real_escape_string($data[11])."')";
mysql_query($import) or die(mysql_error());
Try this
From:
mysql_real_escape_string($data[0])
To:
mysql_real_escape_string(trim($data[0]))

JSON creating from PHP giving wrong data?

I have one php form where i used to enter data to database(phpmyadmin), and i used SELECT query to display all values in database to view in php form.
Also i have another PHP file which i used to create JSON from the same db table.
Here when i enter foreign languages like "Experiența personală:" the value getting saved in DB is "ExperienÈ›a personală: " but when i use select query to display this in same php form it coming correctly "Experiența personală:". So the db is correct and now am using following php code to create JSON
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "aaps";
// Create connection
$con=mysqli_connect($servername,$username,$password,$dbname);
// Check connection
mysqli_set_charset($con, 'utf8');
//echo "connected";
$rslt=mysqli_query($con,"SELECT * FROM offers");
while($row=mysqli_fetch_assoc($rslt))
{
$taxi[] = array('code'=> $row["code"], 'name'=> $row["name"],'contact'=> $row["contact"], 'url'=> $row["url"], 'details'=> $row["details"]);
}
header("Content-type: application/json; charset=utf-8");
echo json_encode($taxi);
?>
and JSON looks like
[{"code":"CT1","name":"Experien\u00c8\u203aa personal\u00c4\u0192: ","contact":"4535623643","url":"images\/offers\/event-logo-8.jpg","details":"Experien\u00c8\u203aa personal\u00c4\u0192: jerhbehwgrh 234234 hjfhjerg#$%$#%#4"},{"code":"ewrw","name":"Experien\u00c8\u203aa personal\u00c4\u0192: ","contact":"ewfew","url":"","details":"eExperien\u00c8\u203aa personal\u00c4\u0192: Experien\u00c8\u203aa personal\u00c4\u0192: Experien\u00c8\u203aa personal\u00c4\u0192: "},{"code":"Experien\u00c8\u203aa personal\u00c4\u0192: ","name":"Experien\u00c8\u203aa personal\u00c4\u0192: ","contact":"","url":"","details":"Experien\u00c8\u203aa personal\u00c4\u0192: "}]
In this "\u00c8\u203aa" this is wrong it supposed to be "\u021b" (t).
So pho used to creating JSON making this issue.
But am unable to find exactly why its coming like this . please help
Avoid Unicode -- note the extra argument:
json_encode($s, JSON_UNESCAPED_UNICODE)
Don't use utf8_encode/decode.
ă turning into ă is Mojibake. It probably means that
The bytes you have in the client are correctly encoded in utf8 (good).
You connected with SET NAMES latin1 (or set_charset('latin1') or ...), probably by default. (It should have been utf8.)
The column in the tables may or may not have been CHARACTER SET utf8, but it should have been that.
If you need to fix for the data it takes a "2-step ALTER", something like
ALTER TABLE Tbl MODIFY COLUMN col VARBINARY(...) ...;
ALTER TABLE Tbl MODIFY COLUMN col VARCHAR(...) ... CHARACTER SET utf8 ...;
Before making any changes, do
SELECT col, HEX(col) FROM tbl WHERE ...
With that, ă should show hex of C483. If you see C384C692, you have "double-encoding", which is messier to fix.
Depending on the version of MySql in the database, it may not be using the full utf-8 set, as stated in the documentation:
The ucs2 and utf8 character sets do not support supplementary characters that lie outside the BMP. Characters outside the BMP compare as REPLACEMENT CHARACTER and convert to '?' when converted to a Unicode character set.
This, however, is not likely to be related to your problem. I would try a couple of different things and see if it solves your problem.
use SET NAMES utf-8
You can read more about that here
use utf8_encode() when inserting data to the database, and utf8_decode() when extracting. That way, you don't have to worry about MySql manipulating the unicode characters. Documentation

Datatables mysql charset circumflex

I have a mysql database with latin1 default character set name.
Through php I save strings in mysql table. From the table I parse some data into tables using DataTables.
Everything work ok, but now I have some problems with circumflex letters and FPDF.
So if I save a string "račun" in the table, the result in the table will be "raÄun".
Or the string "število" will be "Å¡tevilo".
OK -> DataTables decodes those words back normally..
But now when I am using FPDF it gets those string as they are stored in MySQL table and print them "encoded".
I tried
iconv("ISO-8859-1", "ISO-8859-2", "Števika računa")
and
utf8_decode("Števika računa)
But nothing worked.. does anybody has an idea what should I do?
At the end this was the solution:
http://isabelcastillo.com/international-characters-encoding-fpdf

How can I insert Hebrew text into columns? [SQL 2000]

I tried to insert Hebrew a text value into a column,
But it changes the value to Gibberish.
An example of that:
mssql_query ("UPDATE TABLE SET COLUMON = N'בדיקה'");
As you can assume, It changes the value of the column, But the value changed to ????? and if I try to do it from Query Analyser it works fine.
My column's collation is HEBREW_CI_AS. How can I fix this?
You need to specify collation preperty for the string in the INSERT statement you are using. Also the string you are inserting should be of UNICODE datatype - use N prefix for that.
INSERT INTO MEMB_INFO (User, Pass, Name) VALUES ('Joni', '123456', N'גוני דף' COLLATE HEBREW_CI_AS)
Check that PHP variable can handle unicode characters. Otherwise it will be PHP that turns your string into question marks.
You may check out SQL Server drivers for PHP.
And Unicode Character Properties from PHP doicumentation.
Some resources on PHP and unicode:
http://www.sitepoint.com/bringing-unicode-to-php-with-portable-utf8/
http://php.net/manual/en/function.utf8-encode.php
http://allseeing-i.com/How-to-setup-your-PHP-site-to-use-UTF8
http://www.yiiframework.com/wiki/16/how-to-set-up-unicode/
http://pageconfig.com/post/portable-utf8
I solve this problem if someone else has this problem here is my way to fix that:
Create a new database for this specific table or else tables for your web.
Set Hebrew_CI_AS as collation (everyone to what he created).
In your PHP code use mb_convert_encoding() function for SELECT and INSERT.

Categories