473,748 Members | 7,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

input with special characters disappears on mysql insert

4 New Member
Hi all.
I'm having a problem with a special characters.
I have php script that reads from an xml file and writes to a mysql db.
It's a script called phptunest that I found on the net, although the
original website for the author appears to be gone.

It works really nicely except when it hits special characters. Everything from a sp char forward
just gets lost. It is using mysql_real_esca pe_string, but that doesn't seem to help with the
weird characters like (°, é)
For example,
here's a couple of xml entries that get screwed up:

<dict>
<key>Track ID</key><integer>65 83</integer>
<key>Name</key><string>360 ° (Oh Yeah?)</string>
<key>Artist</key><string>Pro pellerheads</string>
.....

<dict>
<key>Track ID</key><integer>13 257</integer>
<key>Name</key><string>Agu a De Bebér</string>
<key>Artist</key><string>Ser gio Mendes & Brasil '66</string>
...

When the script inserts to db then reads them out into html they come out as:

<tr><td width="10%">360 </td>
<td width="10%">&nb sp;</td>
<td width="10%">&nb sp;</td>
<td width="10%">&nb sp;</td>
<td width="10%">0 KB</td>
<td width="10%"></td>
<td width="10%">&nb sp;</td>
<td width="10%">&nb sp;</td>
<td width="10%">&nb sp;</td>
<td width="10%">&nb sp;</td>
</tr>

<tr><td width="10%">Agu a De Beb</td>
<td width="10%">&nb sp;</td>
<td width="10%">&nb sp;</td>
<td width="10%">&nb sp;</td>
<td width="10%">0 KB</td>
<td width="10%"></td>
<td width="10%">&nb sp;</td>
<td width="10%">&nb sp;</td>
<td width="10%">&nb sp;</td>
<td width="10%">&nb sp;</td>
</tr>

I have figured out that the problem is in the sql insert.
When it inserts it to the db everthing in that record from the special characters (°, é)
just gets lost.

Here's the code snippets that do the insert.

// Pushes a single tune into MySQL table
function mysqlPushIntoTa ble(&$tune)
{
if (!isset($tune['Track ID']))
return;
foreach($this->allCols as $col)
{
if(!isset($tune["$col"]))
$tune["$col"] = 0;
$tune["$col"] = str_replace("%" , "\%", mysql_real_esca pe_string($tune[$col]));
}
foreach($tune as $key=>$val)
$tune["$key"] = utf8_decode($va l);
if ($tune['Track ID'] == 0)
die("phptunest error, Track ID is 0! Please report this error to nikita@maczsoft ware.com.<br>") ;
$quer = "INSERT INTO phptunest_tunes values(\"" . $tune['Track ID'] . "\", \"" . $tune['Name'] . "\", \"" . $tune['Artist'] . "\", \"" . $tune['Composer'] . "\", \"" . $tune['Album'] . "\", \"" . $tune['Genre'] . "\", \"" . $tune['Kind'] . "\"," . $tune['Size'] . "," . $tune['Total Time'] . "," . $tune['Disc Number'] . ", " . $tune['Disc Count'] . "," . $tune['Track Number'] . "," . $tune['Track Count'] . "," . $tune['Year'] . "," . $tune['BPM'] . ", " . "\"" . $tune['Date Modified'] . "\", \"" . $tune['Date Added'] . "\", " . $tune['Bit Rate'] . "," . $tune['Sample Rate'] . "," . "\"" . $tune['Equalizer'] . "\", \"" . $tune['Comments'] . "\"," . $tune['Play Count'] . "," . $tune['Play Date'] . "," . "\"" . $tune['Play Date UTC'] . "\", " . $tune['Normalization'] . "," . $tune['Rating'] . "," . $tune['File Type'] . "," . $tune['File Creator'] . "," . "\"" . $tune['Location'] . "\", " . $tune['File Folder Count'] . "," . $tune['Library Folder Count'] . ")";
mysql_query($qu er) or die("Couldn't insert new tune records into database<br>" . mysql_error() . "<br>Query was: $quer<br>");
}

called by:
function xmlCharacterDat a($parser, $data)
{
global $phptunestXmlDe pth,$phptunestA boutToClose, $phptunestValue Coming, $phptunestLastK ey;

if (($phptunestXml Depth == 5) && ($data != ""))
{
if($phptunestAb outToClose) // last entry!
{
$this->curTune['Library Folder Count'] = $data;
if ($this->enableSortin g == 0 && $this->mysqlEnabled == 0)
$this->outputTune($th is->curTune);
else
$this->mysqlPushIntoT able($this->curTune);


...
// Using implode(file()) , because file_get_conten ts() requires
// PHP 4.3, so why bother :)
if (!$tunestr = implode("\n", file($this->file)))
die("Couldn't load XML data");

// We won't use xml_parse_into_ struct() here.
// Let's use event-driven track-by-track parsing, with saving each
// track into MySQL database (this saves LOT of RAM).
xml_set_element _handler($tuneX ML, array($this, "xmlStartElemen t"), array($this, "xmlEndElement" ));
xml_set_charact er_data_handler ($tuneXML, array($this, "xmlCharacterDa ta"));

Can anyone tell me how to fix this?

Many thanks,
Dan
Aug 2 '07 #1
6 3878
kovik
1,044 Recognized Expert Top Contributor
What is the character encoding on your page and your database?
Aug 2 '07 #2
TheRealDan
4 New Member
Hope someone's still around, I got sidetracked for a bit.

I looked and my db encoding just has character-set=latin1

I see that there are numerous options and numerous collation values to add.

How in the world do I figure out which ones to use?

dan
Aug 23 '07 #3
jx2
228 New Member
Hope someone's still around, I got sidetracked for a bit.

I looked and my db encoding just has character-set=latin1

I see that there are numerous options and numerous collation values to add.

How in the world do I figure out which ones to use?

dan
if you dont know which one or you use many languages set every thing (DB and php and server) to UTF-8 thats universal coding (but it takes twice as much space in your database)

regards
jx2
Aug 23 '07 #4
TheRealDan
4 New Member
OK, I've set the default character set to UTF-8 in my php.ini.
(The iTunes XML file has a default of UTF-8)
I also ran an ALTER DATABASE, and an ALTER TABLE command to
set the character set of my mysql db/table to UTF-8.

Reran the php file and I still get the same results. Nothing past the special characters gets into the db (for each xml record).

Any suggestions? Ideas on what I'm missing?

thanks,
Dan
Aug 27 '07 #5
TheRealDan
4 New Member
Hi.
I posted about this a while back, but I still haven't solved it and have some more information.
I've got a php script I found on the net that parses your iTunes xml file, optionally dumps it into a mysql db, then produces an html file of your library.
Tracks that include special characters, like (°, é) get truncated after the special character.

I have the encoding for mysql and php set to UTF-8, same as the xml file.
If I do a direct sql insert of the track info into mysql it goes in just fine.
Also, the script has an option to NOT use mysql, just do it in internal memory, calls "ramParse() "?? - If I use that option, the html file comes out correct, with special characters, etc.

So that seems to tell me that php itself is ok with the sp chars and mysql is ok with them, so it appears to be the mysqlParse routine that is somehow choking on the data and not inserting it correctly. Here is the relevant(?) code, can anyone help?

Expand|Select|Wrap|Line Numbers
  1. // Pushes a single tune into MySQL table
  2. function mysqlPushIntoTable(&$tune)
  3. {
  4. if (!isset($tune['Track ID']))
  5. return;
  6. foreach($this->allCols as $col)
  7. {
  8. if(!isset($tune["$col"]))
  9. $tune["$col"] = 0;
  10. $tune["$col"] = str_replace("%", "\%", mysql_real_escape_string($tune[$col]));
  11. }
  12. foreach($tune as $key=>$val)
  13. $tune["$key"] = utf8_decode($val);
  14. if ($tune['Track ID'] == 0)
  15. die("phptunest error, Track ID is 0! Please report this error to __MUNGED__.<br>");
  16. $quer = "INSERT INTO phptunest_tunes values(\"" . $tune['Track ID'] . "\", \"" . $tune['Name'] . "\", \"" . $tune['Artist'] . "\", \"" . $tune['Composer'] . "\", \"" . $tune['Album'] . "\", \"" . $tune['Genre'] . "\", \"" . $tune['Kind'] . "\"," . $tune['Size'] . "," . $tune['Total Time'] . "," . $tune['Disc Number'] . ", " . $tune['Disc Count'] . "," . $tune['Track Number'] . "," . $tune['Track Count'] . "," . $tune['Year'] . "," . $tune['BPM'] . ", " . "\"" . $tune['Date Modified'] . "\", \"" . $tune['Date Added'] . "\", " . $tune['Bit Rate'] . "," . $tune['Sample Rate'] . "," . "\"" . $tune['Equalizer'] . "\", \"" . $tune['Comments'] . "\"," . $tune['Play Count'] . "," . $tune['Play Date'] . "," . "\"" . $tune['Play Date UTC'] . "\", " . $tune['Normalization'] . "," . $tune['Rating'] . "," . $tune['File Type'] . "," . $tune['File Creator'] . "," . "\"" . $tune['Location'] . "\", " . $tune['File Folder Count'] . "," . $tune['Library Folder Count'] . ")";
  17. mysql_query($quer) or die("Couldn't insert new tune records into database<br>" . mysql_error() . "<br>Query was: $quer<br>");
  18. }
  19.  
  20. called by:
  21. function xmlCharacterData($parser, $data)
  22. {
  23. global $phptunestXmlDepth,$phptunestAboutToClose, $phptunestValueComing, $phptunestLastKey;
  24.  
  25. if (($phptunestXmlDepth == 5) && ($data != ""))
  26. {
  27. if($phptunestAboutToClose) // last entry!
  28. {
  29. $this->curTune['Library Folder Count'] = $data;
  30. if ($this->enableSorting == 0 && $this->mysqlEnabled == 0)
  31. $this->outputTune($this->curTune);
  32. else
  33. $this->mysqlPushIntoTable($this->curTune);
  34.  
  35.  
  36. ...
  37. // Using implode(file()), because file_get_contents() requires
  38. // PHP 4.3, so why bother :)
  39. if (!$tunestr = implode("\n", file($this->file)))
  40. die("Couldn't load XML data");
  41.  
  42. // We won't use xml_parse_into_struct() here.
  43. // Let's use event-driven track-by-track parsing, with saving each
  44. // track into MySQL database (this saves LOT of RAM).
  45. xml_set_element_handler($tuneXML, array($this, "xmlStartElement"), array($this, "xmlEndElement"));
  46. xml_set_character_data_handler($tuneXML, array($this, "xmlCharacterData"));
Oct 23 '07 #6
pbmods
5,821 Recognized Expert Expert
Heya, Dan.

Make sure your database and tables use the UTF character set and also check to see that your connection encoding is UTF.

You can take care of the latter with these two lines:
Expand|Select|Wrap|Line Numbers
  1. mysql_query("SET NAMES 'utf8'");
  2. mysql_query("SET CHARACTER SET 'utf8'");
  3.  
Nov 27 '07 #7

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

Similar topics

3
2525
by: Albretch | last post by:
I am trying to insert some textual data belonging to an HTML page into a table column with 'TEXT' as data type mysql's maual _/manual.html#String_types tell you, you may insert up to (2^16 - 1), that is 65535 characters, but I am getting errors when I try to insert a column larger than 236 characters. Initially I thought it might be because I had to escape some characters, but after playing a some scenarios and just inserting a bunch...
125
14807
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
4
5264
by: Ewok | last post by:
let me just say. it's not by choice but im dealing with a .net web app (top down approach with VB and a MySQL database) sigh..... Anyhow, I've just about got all the kinks worked out but I am having trouble preserving data as it gets entered into the database. Primarily, quotes and special characters. Spcifically, I noticed it stripped out some double quotes and a "Registered" symbol &reg; (not the ascii but the actual character"
5
8630
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file". Same file path containing special characters works fine in one machine, but doesn't work in other. I am using following API function to get short file path. Declare Auto Function GetShortPathName Lib "kernel32" (ByVal lpszLongPath As
9
2560
by: Larry | last post by:
OK, I've been searching around the net for numerous hours and seem to just be getting more confused about handling special characters. In my host's configuration MagicQuotes is ON. (I understand this is considered a bad thing by many) A user submitted an email in the form 'Bob Smith' <bob@nospam.com> Now when I look in the MySql database (via PhpMyAdmin) it's exactly that, but when I try to retrieve it with a standard query, it echo's...
2
3326
by: thisisazam | last post by:
Hi I am picking records from xml file and saving them into mysql database. I am When I insert records, the special characters like ö,å,ä are being transformed into some unwanted values. I tried to insert those values manualy by giving insert into table_name...... this command on command line. It works fine and store even special characters as well. But when I Pick the record from xml and insert them into mysql it does not insert ö,ä,å...
2
5648
by: Ravigandha | last post by:
Hello everybody, My question is how to insert special characters and symbols in Mysql5 database and how to retrieve them from database in php. Here i am inserting some data from a form,by post method like $data=$_post; after this i am inserting in database, mysql_query("insert into tbl_name(column name) values('$data')"); Everthing working fine when we insert simple text in the form,but failing when special character are entered. eg:...
2
3585
by: matech | last post by:
I have a problem with uploading special characters from excel files to mysql 5. It doesn't matter if I use UTF-8 or iso-8859-1 when uploading the trademark ™ symbol. htmlspecialchars() or htmletities() doesn't help? the database doesn't show the data in the field but replaces it with the binary/Image information. the following are examples of how I've tried loading the data along with UTF-8 or iso-8859-1:
1
7888
by: bdparnes | last post by:
I have a project where it is necessary to copy rows (and change a few pieces of information); however some of the rows may contain special characters such as an apostrophie or quotes. I am using the INSERT INTO ... SELECT statement to accomplish the copy fairly quickly... however for those rows with special characters, I get a Syntax error. Is there an easy way around this, or do I need to abandon the INSERT INTO ... SELECT shortcut? ie:...
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.