Hi,
I have a strange issue, i have a txt file on the server with the following text in: - In what ways has 'time-space compression' transformed geographies of consumption and production?
I am using the following to read the document: -
$file = "test.txt";
-
header('Content-Type: text/html; charset=utf-8');
-
$description = file_get_contents($file,0,NULL,0,1500);
-
If i print this out to the page it works fine, however when i do the following: - mysql_query("INSERT INTO `documents` (description) VALUES ('" . $description . "')") or trigger_error(mysql_error(),E_USER_ERROR);
The following is added to the database: - In what ways has ‘time-space compression’ transformed geographies of consumption and production?
Any ideas why this is happening?
Cheers,
Adam
9 3303
Try using mysql_real_escape_string() on your $description variable before passing it to the database.
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.
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?
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: - <?php
-
$file = file_get_contents("test.txt");
-
echo $file;
-
?>
Try that and see what happens. I'd still like to take a look at the text file though.
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
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.
Thanks very much it is much appreciated.
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.
Thanks a lot for that, just put the header on the pages that the description was printed and works fine. Thanks again.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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()
{
|
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)...
|
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...
|
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...
|
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...
|
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"...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |