|
 |
|
excel to mysql ข้อความที่เป็นภาษาไทยไม่เพิ่มเข้าฐานข้อมูลเพิ่มเฉพาะภาษาอังกฤษครับ วอนท่านผู้รู้ช่วยที |
|
 |
|
|
 |
 |
|
Code
INSERT INTO student (id_student,name,lastname,level) VALUES ('1234','','','101') ;
ตรงที่เป็น '','' เป็นภาษาไทยครับ
ส่วนโค๊ดครับ
Code (PHP)
$filename = "excel/".$_FILES["fileCSV"]["name"];
move_uploaded_file($_FILES["fileCSV"]["tmp_name"],$filename); // Copy/Upload CSV
mysql_query("SET NAMES UTF8");
$objCSV = fopen($filename, "r");
while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
$strSQL = "INSERT INTO student ";
$strSQL .="(id_student,name,lastname,level) ";
$strSQL .="VALUES ";
$strSQL .="('".$objArr[0]."','".$objArr[1]."','".$objArr[2]."','".$objArr[3]."') ;";
echo $strSQL.'<br>';
//$objQuery = mysql_query($strSQL);
}
fclose($objCSV);
Tag : PHP

|
|
 |
 |
 |
 |
Date :
2012-07-24 00:17:15 |
By :
TE |
View :
1205 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดูพวก iconv() ทำการ convert ก่อนที่จะ insert ครับ
|
 |
 |
 |
 |
Date :
2012-07-24 06:44:19 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมเอาไฟล์ excel ไปแปลงเป็นไฟล์ชื่ีอ testcsv.csv จากนั้นก็ run code นี้ (น่าจะโค้ดมาจากที่เดียวกัน)
Code (PHP)
<?
$FILE = fopen( "testcsv.csv", "r");//ชื่อไฟล์ และ โหมด r เพื่ออ่านข้อมูลจากไฟล์อย่างเดียว
$data = fgetcsv( $FILE , 1024 );//จะเก็บข้อความไว้ใน Array data แบ่งตามคอลัมน์
$i=1;
do
{
if ($i == 1){
//เพื่อไม่ให้อ่านหัวแถว ลงฐานข้อมูล
$data = fgetcsv( $FILE , 1024 );
$i++;
}
else{
//เริ่มติดต่อฐานข้อมูล
$host="localhost";
$user="apisitp";
$password="xxxxxxxxx";
$dbname="db_xxxxxxxx";
$conetion=mysql_connect($host,$user,$password) or die("ไม่สามารถติดต่อฐ้านข้อมูลได้");
$db=mysql_select_db($dbname) or die("ไม่สามารถเลือกฐานข้อมูลได้");
mysql_query("SET character_set_results=tis620");
mysql_query("SET character_set_client='tis620'");
mysql_query("SET character_set_connection='tis620'");
mysql_query("collation_connection = tis620_thai_ci");
mysql_query("collation_database = tis620_thai_ci");
mysql_query("collation_server = tis620_thai_ci");
$sql="INSERT INTO tb_group (id_g,username,group) VALUES ('$data[0]', '$data[1]', '$data[2]');";
$dbquery=mysql_db_query($dbname,$sql);
mysql_close();
$data = fgetcsv( $FILE , 1024 );
$i++;
}}while ( !feof( $FILE ) );
echo "เรียบร้อย";
?>
ภาษาไทยก็เข้าได้ ตรงนี้ผมเคยไปเจอที่เว็บไหนจำไม่ได้ ทุกครั้งที่จะบันทึกข้อมูลลง db จะต้องมี Code นี้เขียนไว้เสมอ จำไม่ได้ว่าเค้าอธิบายไว้อย่างไร แต่ปัญหาด้านภาษาหมดไปเลยครับ
Code (PHP)
mysql_query("SET character_set_results=tis620");
mysql_query("SET character_set_client='tis620'");
mysql_query("SET character_set_connection='tis620'");
mysql_query("collation_connection = tis620_thai_ci");
mysql_query("collation_database = tis620_thai_ci");
mysql_query("collation_server = tis620_thai_ci");
ลองดูนะครับ
|
 |
 |
 |
 |
Date :
2012-07-24 08:15:10 |
By :
apisitp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังไม่ได้เลยครับ ใครพอมีวิธีบ้างครับ ช่วยหน่อย
|
 |
 |
 |
 |
Date :
2012-07-26 16:26:02 |
By :
TE |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พอรันใน localhost ได้ตามปกติครับแต่พอรันบน Linux ไม่ได้ครับ
|
 |
 |
 |
 |
Date :
2012-07-26 16:37:43 |
By :
TE |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตั้งแต่ตอน Create new database ตอนเลือก Collation เลือก support ภาษาไทยหรือเปล่าครับ
แล้วแต่ละ ฟิลด์ ของตาราง Collation เลือกเป็นที่ support ภาษาไทยหรือเปล่าครับ
ฐานข้อมูล/ตารางผม ผมเลือก tis620_thai_ci ไม่มีปัญหาในเรื่องของภาษาเลยครับ
ไม่รู้ว่าจะเกี่ยวหรือเปล่าลองตรวจสอบดูครับ
|
 |
 |
 |
 |
Date :
2012-07-26 16:42:04 |
By :
apisitp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|