Jump to content

Recommended Posts

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>";

  }

?>

Link to comment
https://forums.phpfreaks.com/topic/145313-csv-to-sql-script-problem/
Share on other sites

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

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.

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

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?

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?

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

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.