talonos Posted February 15, 2009 Share Posted February 15, 2009 ok i've got a script that parses csv info into my sql databae but i'm stuck with it a little as it won't parse one letter and that letter is "Ø" but how do i get my script to parse the letter into the right asci code for html "%C3%98"is the right html acsi code i think but i'm quite a newb at php and can't work out how to do it it works fine with all other symbols i've tried upto now any help would be appreciated <? include "connect.php"; if(isset($_POST['submit'])) { $filename=$_POST['filename']; $handle = fopen("$filename", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $import="INSERT into imba(name,class,spec,role,normal,best,warnings,warnings2,normal2,best2) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[5]','$data[6]','$data[7]','$data[7]','$data[5]','$data[6]')"; mysql_query($import) or die(mysql_error()); } fclose($handle); print "Import done"; } else { print "<form action='index.php' method='post'>"; print "Type file name to import:<br>"; print "<input type='text' name='filename' size='20'><br>"; print "<input type='submit' name='submit' value='submit'></form>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/145313-csv-to-sql-script-problem/ Share on other sites More sharing options...
bothwell Posted February 15, 2009 Share Posted February 15, 2009 Oh man. Character encoding is a real headache, dude, you have my sympathy. What character encoding does your imba table use in the database itself? UTF-8? Something else? Quote Link to comment https://forums.phpfreaks.com/topic/145313-csv-to-sql-script-problem/#findComment-762857 Share on other sites More sharing options...
talonos Posted February 15, 2009 Author Share Posted February 15, 2009 Oh man. Character encoding is a real headache, dude, you have my sympathy. What character encoding does your imba table use in the database itself? UTF-8? Something else? atm i've got it on utf8_unicode_ci but i can change that to whatever if it helps also if it helps the only time it could be used is in $data[0] which is first block within my csv info Quote Link to comment https://forums.phpfreaks.com/topic/145313-csv-to-sql-script-problem/#findComment-762860 Share on other sites More sharing options...
bothwell Posted February 15, 2009 Share Posted February 15, 2009 When you say it doesn't parse, what actually happens? Does your script throw an error of some sort, or does your insert statement just put garbage into your database? There's a couple of things you can do - make sure your CSV file is actually saved in UTF-8 format. Also, open up your connect.php file, and right underneath your mysql_connect(), add the following line: mysql_query("SET NAMES UTF8"); Try those, and let me know what happens. Quote Link to comment https://forums.phpfreaks.com/topic/145313-csv-to-sql-script-problem/#findComment-762888 Share on other sites More sharing options...
talonos Posted February 15, 2009 Author Share Posted February 15, 2009 When you say it doesn't parse, what actually happens? Does your script throw an error of some sort, or does your insert statement just put garbage into your database? There's a couple of things you can do - make sure your CSV file is actually saved in UTF-8 format. Also, open up your connect.php file, and right underneath your mysql_connect(), add the following line: mysql_query("SET NAMES UTF8"); Try those, and let me know what happens. ok i've done as you said earlier and still to no prevail its removing the Ø from the $data[0] string on mysql input and also my csv file is saved in utf-8 format Quote Link to comment https://forums.phpfreaks.com/topic/145313-csv-to-sql-script-problem/#findComment-762905 Share on other sites More sharing options...
bothwell Posted February 15, 2009 Share Posted February 15, 2009 ok i've done as you said earlier and still to no prevail its removing the Ø from the $data[0] string on mysql input and also my csv file is saved in utf-8 format Try changing to utf-general-ci instead, see if anything happens (it probably won't, though). What's the Ø actually being replaced with when it gets into the db? Quote Link to comment https://forums.phpfreaks.com/topic/145313-csv-to-sql-script-problem/#findComment-762909 Share on other sites More sharing options...
talonos Posted February 15, 2009 Author Share Posted February 15, 2009 ok i've done as you said earlier and still to no prevail its removing the Ø from the $data[0] string on mysql input and also my csv file is saved in utf-8 format Try changing to utf-general-ci instead, see if anything happens (it probably won't, though). ok tried changing it to utf-general-ci with no joy and also in the mysql database its actually not inputing any for the Ø char at all What's the Ø actually being replaced with when it gets into the db? Quote Link to comment https://forums.phpfreaks.com/topic/145313-csv-to-sql-script-problem/#findComment-762911 Share on other sites More sharing options...
bothwell Posted February 15, 2009 Share Posted February 15, 2009 Ok - change your script to this: <? include "connect.php"; if(isset($_POST['submit'])) { $filename=$_POST['filename']; $handle = fopen("$filename", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { print $data[0]; // $import="INSERT into imba(name,class,spec,role,normal,best,warnings,warnings2,normal2,best2) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[5]','$data[6]','$data[7]','$data[7]','$data[5]','$data[6]')"; // mysql_query($import) or die(mysql_error()); } fclose($handle); print "Import done"; } else { print "<form action='index.php' method='post'>"; print "Type file name to import:<br>"; print "<input type='text' name='filename' size='20'><br>"; print "<input type='submit' name='submit' value='submit'></form>"; } ?> And tell me if the non-latin character in the name gets printed out properly to the browser? Quote Link to comment https://forums.phpfreaks.com/topic/145313-csv-to-sql-script-problem/#findComment-762920 Share on other sites More sharing options...
talonos Posted February 15, 2009 Author Share Posted February 15, 2009 Ok - change your script to this: <? include "connect.php"; if(isset($_POST['submit'])) { $filename=$_POST['filename']; $handle = fopen("$filename", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { print $data[0]; // $import="INSERT into imba(name,class,spec,role,normal,best,warnings,warnings2,normal2,best2) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[5]','$data[6]','$data[7]','$data[7]','$data[5]','$data[6]')"; // mysql_query($import) or die(mysql_error()); } fclose($handle); print "Import done"; } else { print "<form action='index.php' method='post'>"; print "Type file name to import:<br>"; print "<input type='text' name='filename' size='20'><br>"; print "<input type='submit' name='submit' value='submit'></form>"; } ?> And tell me if the non-latin character in the name gets printed out properly to the browser? ok i done that n the EXACT output i got was nixImport done Quote Link to comment https://forums.phpfreaks.com/topic/145313-csv-to-sql-script-problem/#findComment-762932 Share on other sites More sharing options...
bothwell Posted February 15, 2009 Share Posted February 15, 2009 What the hell? ??? Well, I've no idea how to fix the encoding, but we can convert the character to its html entity, I guess. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $data = htmlentities($data); $import="INSERT into imba(name,class,spec,role,normal,best,warnings,warnings2,normal2,best2) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[5]','$data[6]','$data[7]','$data[7]','$data[5]','$data[6]')"; mysql_query($import) or die(mysql_error()); } This should get it into your database at least. Quote Link to comment https://forums.phpfreaks.com/topic/145313-csv-to-sql-script-problem/#findComment-762942 Share on other sites More sharing options...
talonos Posted February 15, 2009 Author Share Posted February 15, 2009 What the hell? ??? Well, I've no idea how to fix the encoding, but we can convert the character to its html entity, I guess. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $data = htmlentities($data); $import="INSERT into imba(name,class,spec,role,normal,best,warnings,warnings2,normal2,best2) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[5]','$data[6]','$data[7]','$data[7]','$data[5]','$data[6]')"; mysql_query($import) or die(mysql_error()); } This should get it into your database at least. Well i've tried everything i can think of now n tbh everytime that char comes up ima just input it manually via phpadmin until a confirmed fix inside php itself is founsd as reading up on it php has allot of problems reading danish chars from files like csv and xml thanks for helping me guys Quote Link to comment https://forums.phpfreaks.com/topic/145313-csv-to-sql-script-problem/#findComment-762982 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.