473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Strange characters when importing text file into database?

133 100+
Hi,

I have a strange issue, i have a txt file on the server with the following text in:

Expand|Select|Wrap|Line Numbers
  1. In what ways has 'time-space compression' transformed geographies of consumption and production?
I am using the following to read the document:

Expand|Select|Wrap|Line Numbers
  1.     $file = "test.txt"; 
  2.     header('Content-Type: text/html; charset=utf-8');
  3.     $description = file_get_contents($file,0,NULL,0,1500);
  4.  
If i print this out to the page it works fine, however when i do the following:

Expand|Select|Wrap|Line Numbers
  1. mysql_query("INSERT INTO `documents` (description) VALUES ('" . $description . "')") or trigger_error(mysql_error(),E_USER_ERROR);
The following is added to the database:

Expand|Select|Wrap|Line Numbers
  1. In what ways has â€˜time-space compression’ transformed geographies of consumption and production?
Any ideas why this is happening?

Cheers,
Adam
Mar 10 '11 #1
9 3303
JKing
1,206 Expert 1GB
Try using mysql_real_escape_string() on your $description variable before passing it to the database.
Mar 10 '11 #2
HaLo2FrEeEk
404 256MB
Two things, you don't need to use all of the parameters in file_get_contents. If you just want to read the entire file, just do this:

$file = file_get_contents("filename");

Also, this is an issue with a difference between the character set of the text file and the character set of the page printing the mysql output. I had a similar issue where the return from JSON API was printing apostrophe characters as ‘. I figured out that it was because the JSON used the UTF-8 charset and my page wasn't. Simply sending a Content-Type header and setting the charset doesn't change the charset of the text file.
Mar 10 '11 #3
adamjblakey
133 100+
Thank you for all your replies. I tried using mysql_real_escape_string before being inserted but this did not work. Also in regards to the character set do you know how i can resolve this. The txt file is already present on the server, is there a way to open this and read while setting the character set?
Mar 11 '11 #4
HaLo2FrEeEk
404 256MB
Can you send me the text file? I'd like to look at it in a hex editor. You could try using file_get_contents() to read the file, then echo it out (without doing any database inserts). See if the odd characters show up. You should have just this code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $file = file_get_contents("test.txt");
  3. echo $file;
  4. ?>
Try that and see what happens. I'd still like to take a look at the text file though.
Mar 11 '11 #5
adamjblakey
133 100+
Hi Thanks for the fast reply, i have attached the file for you to have a look at. When i print the file to the page it comes out fine. It is just the process of adding the file to the database the problem occurs.

226498549.txt
Mar 11 '11 #6
HaLo2FrEeEk
404 256MB
Ok, I work til 10 central time tonight, but I'll take a look when I get home. It might have to do with the character encoding of the actual database table. If you have access to phpMyAdmiin, do the insert then look at the row's contents. I'll see what I can figure out when I get home.
Mar 11 '11 #7
adamjblakey
133 100+
Thanks very much it is much appreciated.
Mar 11 '11 #8
HaLo2FrEeEk
404 256MB
Dude, I apologize for how long it took me to get to this. I've been super tired coming home from work lately and haven't gotten much done.

Anyway, the first thing I notice when I look at the file is that it is UTF-8 encoded, all of the text files I went through on my computer are ANSI encoded. UTF-8 should still be what you want though. The next thing is that the apostrophes aren't really apostrophes at all, they're grave and acute accents. an apostrophe is a single, vertical line: ', a accents look similar, but the're slanted: ` and ´. This could very well be what's causing the problems.

I went ahead and set up a test table in my database and inserted just the first few sentences of the text file. I echoed out the text directly from the file_get_contents() call and at first it had the weirdness, but then I added the header to set the charset and it worked. I inserted it into the database and looked at it in phpMyAdmin and the strange characters were there, but when I pulled it out and echoed it again, it was how it should be, with the accents. I think that the character are alwys going to get messed up when you put them in the database, that's just their unicode representation, but ifyou set the charset to utf-8 on any page where you print the output from the database, it should be fine.
Mar 14 '11 #9
adamjblakey
133 100+
Thanks a lot for that, just put the header on the pages that the description was printed and works fine. Thanks again.
Mar 14 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jay O'Connor | last post by:
I'm using Python 2.3 on windows and trying to use the win32 libs. I keep getting the following error >>> import win32gui Traceback (most recent call last): File "<pyshell#6>", line 1, in...
11
by: David Lozzi | last post by:
Hello, I need to automate importation of a excel file into a table. Here's my scenario: I'm writing an ASP.NET application where users can pull reports on imported data. The imported data is...
1
by: Tim Gains | last post by:
can someone instruct me as to how to load a binary file and the import it's data (rows pf data) into a database? Do I need to convert the binary file in any way in order to decipher it's data. Any...
2
by: Joneleth | last post by:
Hi I was writing some aspx pages when i experimented a strange issue. I have a div tag like this: <div id="divCourses1" style="<%=GetDisplayStyle(eidlevel, arrIdLevel)%>"> Now, as long as i...
0
by: Wescotte | last post by:
<?php global $TABLE_GL_DATA; global $connect; $PREPARED_SQL = odbc_prepare($connect, "INSERT INTO $TABLE_GL_DATA VALUES (?,?,?,?,?,?,?)"); function Generate_GL_Data() {
2
by: qarmoe | last post by:
This code is to import bunch of excel file into access as a single table. table name is same as file name. it works for the first file and then breaks where i have Set TDF = DB.TableDefs(TempFile)...
1
by: ahammad | last post by:
This application opens a Word document for editing. I need to keep the file path of the Word document even after it has been closed. I tried using Word event handlers but I couldn't figure it out, so...
3
by: Shisou | last post by:
Hey bytes community! this one is a really strange issue I ran into and I'm hoping you all can shine some light on it for me. This is written in C, not C++ I'm working on a program that reads...
3
by: Vish4u | last post by:
Hello Everyone, I have a encountered a strange issue with the execution of my stored procedure on clients machine. My stored procedure contains a cursor in which there is a select statement...
6
by: passionateforjava | last post by:
Hi All, I am using struts application wherein I need to import file for some purpose.I have used input type="file" for the same which goes like: <input type="file" id="uploadFile" name="uploadFile"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.