|
 |
|
ขอ code สำหรับ import file .csv to Mysql แบบไม่เจาะจงไฟล์ค่ะ |
|
 |
|
|
 |
 |
|
แบบไหนครับ ขอรายละเอียดหน่อยด้วยครับ
|
 |
 |
 |
 |
Date :
2009-11-03 13:02:11 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือโดยที่ไม่มีการเตรียมไฟล์ไว้ก่อนหน้าอ่ะค่ะ
ผู้ใช้สามารถเลือกไฟล์จากที่ไหนก้อได้
พี่ช่วยดูโค๊ดอันนี้ให้หน่อยค่ะคือหนูทำสามารถอัพโหลดไฟล์ได้
แต่ไม่ลงฐานข้อมูล(msql)ค่ะ
Code
if(isset($_POST['send']))
{
//อ่านไฟล์ csv แล้วทำการ insert ลง database
$row = 1;
$fileCSV = $fileupload;
$ContactList = $_POST["InsertTostudent"];
$pathFileCSV = "FileStore/FileCSV/";
$FileSource = $pathFileCSV.$fileCSV ;
//$oodNum = n = 2*$k+1;
$host="localhost";
$user="root";
$password="1234";
$dbname="alumni";
$conetion=mysql_connect($host,$user,$password) or die("ไม่สามารถติดต่อฐานข้อมูลได้");
$objCSV = fopen($FileSource, "r") or die("Can't open CSV file");
//วนลูปเพื่อ inser ลง database ครั้งละ record
while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
$strSQL = "INSERT INTO student";
$strSQL .="(student_ID,student_name,student_lastname,major_id,adviser_id,address,phone,email) ";
$strSQL .="VALUES ";
$strSQL .="('".$objArr[0]."','".$objArr[1]."','".$objArr[2]."' ,'".$objArr[3]."' ";
$strSQL .=",'".$objArr[4]."','".$objArr[5]."','".$objArr[6]."','".$objArr[7]."') ";
$objQuery = mysql_query($strSQL);
|
 |
 |
 |
 |
Date :
2009-11-03 13:38:32 |
By :
nutthip |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
งง ครับผม
|
 |
 |
 |
 |
Date :
2009-11-03 14:48:14 |
By :
tingtongkub |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (phpCSVMySQLForm.php)
<html>
<head>
<title>ThaiCreate.Com PHP & CSV To MySQL</title>
</head>
<body>
<form action="phpCSVMySQLUpload.php" method="post" enctype="multipart/form-data" name="form1">
<input name="fileCSV" type="file" id="fileCSV">
<input name="btnSubmit" type="submit" id="btnSubmit" value="Submit">
</form>
</body>
</html>
Code (phpCSVMySQLUpload.php)
<html>
<head>
<title>ThaiCreate.Com PHP & CSV To MySQL</title>
</head>
<body>
<?
copy($_FILES["fileCSV"]["tmp_name"],$_FILES["fileCSV"]["name"]); // Copy/Upload CSV
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database"); // Conect to MySQL
$objDB = mysql_select_db("mydatabase");
$objCSV = fopen("customer.csv", "r");
while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
$strSQL = "INSERT INTO customer ";
$strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";
$strSQL .="VALUES ";
$strSQL .="('".$objArr[0]."','".$objArr[1]."','".$objArr[2]."' ";
$strSQL .=",'".$objArr[3]."','".$objArr[4]."','".$objArr[5]."') ";
$objQuery = mysql_query($strSQL);
}
fclose($objCSV);
?>
</table>
</body>
</html>
|
 |
 |
 |
 |
Date :
2009-11-03 15:00:58 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมขอถามนิดนึงนะครับ
ผม import เข้านะครับ แต่ว่า font มันเป็นภาษาต่าง ดาวครับ
ผมสร้างฟิลด์ให้รองรับเป็น varchar(utf8_unicode_ci) นะครับ
แล้วผมก็ใช้ code ข้างบน แต่ font ไทยที่ import เข้าไป มันเป็นแบบนี้อ่ะครับ
¹Ò ¡Í§¾Å ¹Ò ¾á·Â§´ ********
ไม่ทราบต้องแก้ไขตัวไหนครับ ขอบคุณครับ
ลืมบอกไปครับ ถ้าผมใช้ mysql_query("SET NAMES UTF8"); มันจะไม่ import ภาษาไทยเข้าไปเลยครับ
แต่ถ้าไม่ใส่เป็นต่างด้าวเหมือนข้าบนครับ
|
 |
 |
 |
 |
Date :
2010-03-17 09:49:44 |
By :
tongmew02 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ ผมก็อยากทำได้แต่ทำไงก้ไม่ได้สักทีที่นี้ได้ละ
|
 |
 |
 |
 |
Date :
2010-03-17 11:08:57 |
By :
theprincemp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|