How to trim leading spaces while importing Excel into mysql - php

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]))

Related

How to import Romanian diacritics from csv to mysql database

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.

importing CSV file to mysql, there is in persian(utf-8) formate text

Structure of mysql table:
id-----int
name---var-----utf8_persian_ci
after saveing the EXCEL file as a file.csv and importing it from phpmyadmin
it's showing the text in database like:
????? ??? ???? 䟪? 㠧?驥
even my mysql formate is in utf8, here when i save the text in csv and if i open csv file in notepad still it's like:
????? ??? ???? 䟪? 㠧?驥
how i can keep it work in utf8, regards in advacen
For persian excel files on importing it to mysql,
file name = file1.xls
1- the database table columns should support utf8-persian-ci
2- save .xls fiel as **Unicode Text(.txt)**--file1.txt
3- open file1.txt (now replace tab(space) to comma(,)
you can do it by search and replace just copy the tab(space) and replace with comma(,)
4- after replaceing tab to comma , **save as** file as file1.csv
on encodeing--select utf-8
4- open phpmyadmin ,create database ,create table(same column)
5- chose file1.csv
6- run import
here you are done.
NOTE: the column of your table in database should be same as column in file1.csv
regards

Excel CSV numbers to normal string for PHP

I'm working on an import module where an admin can import rows of client information (including the client's ID Number which is a 13 digit numeric value) from a CSV file.
The problem appears that Excel converts the numbers to look like 8.88888E+12 if the value was 8888881111088 and I need to validate that number (even if it's a string) to match back to a previous entry in the database).
Unfortunately I cannot rely on the admin to convert these entries within Excel to a string so I am looking for a way in PHP to convert the incorrect values to a 13-digit number/string.
Any suggestions would greatly be appreciated!
Here is a sample of the script I am using:
<?php
// $data contains the rows returned from the CSV file:
foreach ($data AS $k => $v) {
/* $v['id_number'] is 8.88888E+12 but in the database it's stored correctly
* as 888888111088. I need to match the imported value from the CSV to the row
* in the database.
*
* I need to convert 8.88888E+12 back to 888888111088.
*/
if (strlen(str_replace(" ", "", $v['id_number'])) != 13) {
$this->importValidateRules[$k] = "The ID number (" . $v['id_number'] . ") is invalid. It must be 13 characters long.";
}
// Continue with model validation:
}
?>
In general putting ' sign in cell before the number could enforce Excel to use the value as text. If you could manipulate the original xls or ask user to do it before saving to csv it would work {e.g. having a new column in and putting onto B1 cell =CONCATENATE("'",A1) }
"Figuring out" from csv+php won't work (precision lost already).

strange character inserted in table utf8

I have a site developed in codeigniter where I'd like to insert a record inside my database in a table with utf8 fields.
The problem is that when I insert something inside the table I see that:
�"�h�t�t�p�:�/�/�I�m�a�g�e�1�.�u�r�l�f�o�r�i�m�a�g�e�s�.�c�o�m�/�D�e�f�a�u�l�t�/�8�8�0�4�/�2�3�6�9�2�2�6�2�-�1�8�4�3�3�8�5�2�6�6�.�j�p�g�"�
There are many more characters. The real string is a simple path. I don't know the format of the string because it is from an external server.
This is my query to insert record. I take the string from xml and if I print it inside the page I see the correct string. The problem occurs whenever I check inside the database:
foreach($img->childNodes as $node){
$data = array(
'image'=>$node->getAttribute('path'),
);
$this->db->insert('hotel_images',$data);
}
That data is not UTF-8. It is UCS-2 or UTF-16. UCS-2 is a subset of UTF-16, so treating it as UTF-16 should do the trick.
You can convert it using iconv.
$data = iconv("UTF-16", "UTF-8", $data);

modify csv structure using PHP before import to mysql

I have a csv in .txt who need to be imported to MySql
I'm importing it manualy to my table but it keep breaking giving me this error :
Invalid column count in CSV input on line 706.
checking it on excel the only information who could make the problem is that one of the cells contains : 5.5 x 18"
probably the quotation mark is doing it
how can i fix this for the import ?
thank you
I believe all you need is this:
str_replace('"', '\"', $value);
By replacing the " with \" you are escaping it and it should insert properly.

Categories