0
4

3 回答 3

1

That's most likely encoding problem.
Either proper encoding is not set or some of your apostrophes got encoded manually.

Make sure that your table have utf8 encoding on them
make sure you're calling

$db->set_charset('utf8') 

after connect.
(after doing that write NEW apostrother to database and try to read it back)

Make sure your code doesn't perform useless encoding like htmlspecialchars or so:

  • select your problematic value from database, then urlencode() it
  • read it's counterpart from CSV value and urlencode() it
  • compare the results
于 2013-02-22T05:03:14.297 回答
0

This solved the problem.

<?php
    function utf8_fopen_read($fileName) { 
        $fc = iconv('windows-1250', 'utf-8', file_get_contents($fileName)); 
        $handle=fopen("php://memory", "rw"); 
        fwrite($handle, $fc); 
        fseek($handle, 0); 
        return $handle; 
    } 
?>

Demo:

<?php
    ini_set('default_charset','utf-8');
    header('content-type: text/plain');
    $sql = new mysqli('localhost','root','','test');
    $sql->set_charset('utf8');

    $rows = array();

    $fh = utf8_fopen_read("sample.txt"); 
    while (($data = fgetcsv($fh, 1000, ",")) !== false) { 
        foreach ($data as $value) { 
           $rows[] = $value;
        } 
    }       

    foreach($rows as $row){     
        $row  = trim($row);
        $Name = $row;

        print "$Name\r\n";  
    }

    print "-----------------\r\n";

    foreach($rows as $row){     
        $Name  = trim($row);

        $stmt = $sql->prepare("SELECT ID,Name FROM `establishment` WHERE Name=?");
        $stmt->bind_param("s",$Name);
        $stmt->execute() or die($stmt->error);
        $stmt->store_result();
        if($stmt->num_rows==0){
            // do insert query
        }
        else{
            $stmt->bind_result($ID,$Name);
            $stmt->fetch();
            print "$ID:$Name already exist\r\n";
        }
    }
  ?>

Save as sample.txt (tab-delimiter file):

Adams’ Inn  
Ambassador Inn a
Amberley Suite 
America’s Best Inn
Amber's Inn
于 2013-02-22T08:40:04.943 回答
-1

The curved apostrophe is an abomination of Microsoft. This link explains how to convert them back to single quotes as they should be: http://www.toao.net/48-replacing-smart-quotes-and-em-dashes-in-mysql

于 2013-02-22T06:06:20.453 回答