אשכול: קידוד ב-mySQL
View Single Post
ישן 06-03-08, 15:20   # 4
בניה
משתמש - היכל התהילה
 
מיני פרופיל
תאריך הצטרפות: Oct 2005
מיקום: נחושה
הודעות: 3,434

בניה לא מחובר  

ה SET NAMES זו שאילתה שקובעת את הקידוד של החיבור שלך עם המסד נתונים.
אם הקידוד של החיבור הוא latin1 ואתה מנסה להעביר UTF8 או ההפך אתה עלול לקבל סימני שאלה/גי'בריש.

וגם אתה חייב שהcollation במסד יוגדר כמו שצריך לפני שאתה מכניס אליו מידע בUTF8.

קוד שעבד לי:

PHP קוד:
<?php
error_reporting
(E_ALL);

set_time_limit(600);

    function 
sql_query($query)
    {
        global 
$con;
        
$trace debug_backtrace();

        
$file = &$trace[0]['file'];
        
$line = &$trace[0]['line'];

        
$res mysql_query($query$con);
        if(!
$res)
             die(
"\r\nSQL ERROR: [".$query."|".mysql_error()."] in "$file ." AT ".$line);
        else
            return 
$res;
    }

    function 
convert($str) {

        return 
iconv("windows-1255""UTF-8"$str);

    }

    function 
sql_escape($str){
        global 
$con;
        return 
mysql_real_escape_string($str$con);
    }

$db 'conv';

$table 'lalal';

$con mysql_connect('localhost''root');

mysql_select_db($db$con);

sql_query("SET NAMES 'hebrew'"$con);

$sql "SELECT * FROM " $db "." $table "";

$res sql_query($sql);

sql_query("SET NAMES 'UTF8'"$con);

$fields false;

$c 0;
$values = array();

$rows_num mysql_num_rows($res);

while(
$row mysql_fetch_assoc($res)) {


    if(
$c == 0) {
        
$sql "INSERT INTO `new_{$table}` (`" implode('`,`'array_keys($row)) . "`) VALUES ";

    }

    
$c++;

    
$row array_map('convert'$row);
    
$row array_map('sql_escape'$row);

    
$values[] = "('" implode("','"$row) . "')";

    if((!
$c%10) || $c == $rows_num) {
        
sql_query($sql implode(','$values));
        
$values = array();
    }
}


echo 
"{$c} rows converted";
?>
(אני צריך אחד גם לעצמי)
  Reply With Quote