utf8.php
發表於 : 2008-08-21 21:15:47
<?php
include("common.php");
//sql 轉換
function tosql1($value, $type){
if(!strlen($value)){
return "''";
}else{
if($type == "Number"){
return str_replace (",", ".", doubleval($value));
}else{
$value = str_replace("'","''",$value);
$value = str_replace('"','\"',$value);
$value = str_replace("\\","\\\\",$value);
$value = str_replace("\\'","''",$value);
$value = str_replace("\\\"","\"",$value);
}
return "'" . $value . "'";
}
}
//db1 為 UTF資料庫
$db1 = new DB_Sql();
$db1->Database = "utf8";
$db1->User = "root";
$db1->Password = "";
$db1->Host = "10.10.70.31";
$db1->query("SET NAMES 'UTF8'");
$not_tran = array(
"coupon",
"login_log",
"prod_stock",
"stock_change",
"stock_history"
);
echo "<pre>";
echo date("Y-m-d H:i:s")."<br>";
//讀出所有的資料表
$tables_a1 = $db->table_names();
foreach($tables_a1 as $key => $value){
if(!in_array($value["table_name"], $not_tran)){
$tables_a[] = $value["table_name"];
}
}
foreach($tables_a as $tkey => $table){
//若db1不存在才做
$query1 = "select * from $table";
$db1->query($query1);
if($db1->num_rows()==0){
//讀出所有資料結構
$metadata = $db->metadata($table);
$query = "select * from $table";
$db->query($query);
while($db->next_Record()){
$sql = "insert $table set ";
foreach($metadata as $fkey => $field){
$sql .= $field["name"]." = ".tosql1(iconv("big5","utf-8",$db->f($field["name"])),"Text").",";
}
$sql = substr($sql,0,-1);
//echo $sql."\n";
$db1->query($sql);
}
}
}
echo date("Y-m-d H:i:s")."<br>";
?>
include("common.php");
//sql 轉換
function tosql1($value, $type){
if(!strlen($value)){
return "''";
}else{
if($type == "Number"){
return str_replace (",", ".", doubleval($value));
}else{
$value = str_replace("'","''",$value);
$value = str_replace('"','\"',$value);
$value = str_replace("\\","\\\\",$value);
$value = str_replace("\\'","''",$value);
$value = str_replace("\\\"","\"",$value);
}
return "'" . $value . "'";
}
}
//db1 為 UTF資料庫
$db1 = new DB_Sql();
$db1->Database = "utf8";
$db1->User = "root";
$db1->Password = "";
$db1->Host = "10.10.70.31";
$db1->query("SET NAMES 'UTF8'");
$not_tran = array(
"coupon",
"login_log",
"prod_stock",
"stock_change",
"stock_history"
);
echo "<pre>";
echo date("Y-m-d H:i:s")."<br>";
//讀出所有的資料表
$tables_a1 = $db->table_names();
foreach($tables_a1 as $key => $value){
if(!in_array($value["table_name"], $not_tran)){
$tables_a[] = $value["table_name"];
}
}
foreach($tables_a as $tkey => $table){
//若db1不存在才做
$query1 = "select * from $table";
$db1->query($query1);
if($db1->num_rows()==0){
//讀出所有資料結構
$metadata = $db->metadata($table);
$query = "select * from $table";
$db->query($query);
while($db->next_Record()){
$sql = "insert $table set ";
foreach($metadata as $fkey => $field){
$sql .= $field["name"]." = ".tosql1(iconv("big5","utf-8",$db->f($field["name"])),"Text").",";
}
$sql = substr($sql,0,-1);
//echo $sql."\n";
$db1->query($sql);
}
}
}
echo date("Y-m-d H:i:s")."<br>";
?>