|
|
|
ช่วยตรวจสอบโค้ด ให้หน่อยครับ เพราะนำข้อมูลเข้าฐาน แล้วไม่เป็นภาษาไทย |
|
|
|
|
|
|
|
Code (PHP)
<?php
/*
* Part 2 code modified by Clint Christopher Canada from different public domain sources
* Part 1 code created by Clint Christopher Canada. BSD licensed.
*/
// This is Part I of the code
$tbl = "r506";
$db_uname = 'sa';
$db_passwd = 'xxxxxx';
$db = 'survildb';
$conn = mysql_pconnect('localhost',$db_uname, $db_passwd);
//mysql_query("SET NAMES tis620",$conn);
//mysql_query("SET NAMES UTF8",$conn);
mysql_select_db($db,$conn) or die ("Could not select $db database");
// Path to dbase file
$db_path = "uploads/r506.dbf";
// Open dbase file
$dbh = dbase_open($db_path, 0)
or die("Error! Could not open dbase database file '$db_path'.");
// Get column information
$column_info = dbase_get_header_info($dbh);
// Display information
//print_r($column_info);
$line = array();
foreach($column_info as $col)
{
switch($col['type'])
{
case 'character':
$line[]= "`$col[name]` VARCHAR( $col[length] )";
break;
case 'number':
$line[]= "`$col[name]` FLOAT";
break;
case 'boolean':
$line[]= "`$col[name]` BOOL";
break;
case 'date':
$line[]= "`$col[name]` DATE";
break;
case 'memo':
$line[]= "`$col[name]` TEXT";
break;
}
}
$str = implode(",",$line);
$sql = "CREATE TABLE `$tbl` ( $str );";
//set db utf8
//mb_internal_encoding('utf8');
//mysql_query("SET CHARACTER SET 'utf8'");
//mysql_query("SET NAMES 'utf8'");
mb_internal_encoding('utf8');
mysql_query("SET CHARACTER SET 'tis-620'");
mysql_query("SET NAMES 'tis-620'");
//set db utf8
mysql_select_db($db,$conn);
mysql_query($sql,$conn);
set_time_limit(0); // I added unlimited time limit here, because the records I imported were in the hundreds of thousands.
// This is part 2 of the code
import_dbf($db, $tbl, $db_path);
function import_dbf($db, $table, $dbf_file)
{
global $conn;
if (!$dbf = dbase_open ($dbf_file, 0)){ die("Could not open $dbf_file for import."); }
$num_rec = dbase_numrecords($dbf);
$num_fields = dbase_numfields($dbf);
$fields = array();
for ($i=1; $i<=$num_rec; $i++){
$row = @dbase_get_record_with_names($dbf,$i);
$q = "insert into $db.$table values (";
foreach ($row as $key => $val){
if ($key == 'deleted'){ continue; }
$q .= "'" . addslashes(trim(mb_convert_encoding($val,"UTF-8","BIG5"))) . "',"; // Code modified to trim out whitespaces (BIG5 to UTF-8)
}
if (isset($extra_col_val)){ $q .= "'$extra_col_val',"; }
$q = mb_substr($q, 0, -1);
$q .= ')';
if (!$result = mysql_query($q, $conn)){
print (mysql_error() . " SQL: $q
\n");
print (substr_count($q, ',') + 1) . " Fields total.";
$problem_q = explode(',', $q);
$q1 = "desc $db.$table";
$result1 = mysql_query($q1, $conn);
$columns = array();
$i = 1;
while ($row1 = mysql_fetch_assoc($result1)){
$columns[$i] = $row1['Field'];
$i++;
}
$i = 1;
foreach ($problem_q as $pq){
print "$i column: {$columns[$i]} data: $pq
\n";
$i++;
}
die();
}
}
}
?>
Tag : PHP, MySQL
|
|
|
|
|
|
Date :
2013-08-15 16:17:03 |
By :
pphat21 |
View :
966 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เปลี่ยนไปใช้ UTF-8 ให้หมดครับ
แก้ปัญหาภาษาไทย php กับ MySQL (TIS-620 กับ UTF-8)
|
|
|
|
|
Date :
2013-08-16 09:08:45 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใช้ SET NAMES 'tis-620' หน้าเว็บคุณก็ควร encoding windows874 หรือ tis620 ด้วยน่ะ
น่าใช้พวก SET NAMES 'UTF8' แล้วหน้าเว็บก็ encoding เป็น utf-8 น่าจะเหมาะกว่าน่ะครับ
|
|
|
|
|
Date :
2013-08-16 09:17:52 |
By :
mangkunzo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|