php - Problems inserting strings in MySQL -
well, ill try explain please, apologize english.
i have script dumps entire database sql file , script splits lines , execute them drop, create , insert data. problem strings "trimmed". insert string until reach first special character, example:
for string:
"pantalon azul marino de poliéster con cinta blanca bordada con el nombre de la institución en uno de sus costados."
it insert:
"pantalon azul marino de poli"
no error thrown. happens using script, when run queries manually , importing sql file in phpmyadmin works. set utf8 way.
i'm out of ideas, appreciated.
include ('../core/connection.inc.php'); $conn = dbconnect('admin'); $conn->query("set names 'utf8'"); $conn->set_charset("utf8"); $type = 0; // temporary variable, used store current query $templine = ''; // read in entire file $lines = file('db-backup.sql'); // loop through each line $correct = 0; $failed = 0; foreach ($lines $line){ // skip if it's comment if (substr($line, 0, 2) == '--' || $line == '') continue; // add line current segment $templine .= $line; // if has semicolon @ end, it's end of query if (substr(trim($line), -1, 1) == ';'){ $templine = str_replace("latin1","utf8",$templine); $templine = trim($templine); // perform query $conn->query($templine); $errno = $conn->errno; $error = $conn->error; if($conn->affected_rows > 0){ echo "ok: (".$templine.")<br/>"; $correct++; } else { echo "failed: (".$templine.")<br/>"; echo " errno: ".$errno." <br/>"; echo " error: ".$error." <br/>"; $failed++; } $templine = ''; } }
i'm guessing dump file you're importing isn't utf-8.
php piping bytes file mysql without conversion. é
character in file in latin1
based on change you're making, represented single byte value > 127. isn't utf-8. you've promised mysql you'll send valid utf-8, , stops reading string when gets invalid byte.
you might consider:
- re-encoding dump file utf-8
- figuring out encoding dump file in, , loading mysql using encoding
personally think i'd approach problem different way:
- load dump file mysql using command-line client, or similar. know works.
- alter character set of each column after importing - can use data in
information_schema
assemblealter table
statements , mysql conversion properly.
Comments
Post a Comment