ดั้มข้อมูลแล้ว ภาษาไทยไม่เข้าดาตาเบส Import Excel To Mysql Database Using PHP
ดั้มข้อมูลจากไฟล์ csv แล้ว ภาษาไทยไม่เข้าดาตาเบส (ภาษาอังกฤษและตัวเลขเข้า) ถ้าเเก้ข้อมูลกลับโดยเเก้เป็นภาษาอังกฤษข้อมูลก็เข้าปกติ ไม่ทราบว่าเป็นเพราะอะไรครับ
ดาต้าเบสใช้ฟรอน utf8
code เพิ่มเติ่ม
db.php
Code (PHP)
<?php
$conn=mysql_connect("localhost","root","root") or die("Could not connect");
mysql_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci';");
mysql_select_db("db1",$conn) or die("could not connect database");
?>
index
Code (PHP)
<!DOCTYPE html>
<?php
include 'db.php';
?>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Import Excel To Mysql Database Using PHP </title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Import Excel File To MySql Database Using php">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-responsive.min.css">
<link rel="stylesheet" href="css/bootstrap-custom.css">
</head>
<body>
<!-- Navbar
================================================== -->
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="#">Import Excel To Mysql Database Using PHP</a>
</div>
</div>
</div>
<div id="wrap">
<div class="container">
<div class="row">
<div class="span3 hidden-phone"></div>
<div class="span6" id="form-login">
<form class="form-horizontal well" action="import.php" method="post" name="upload_excel" enctype="multipart/form-data">
<fieldset>
<legend>Import CSV/Excel file</legend>
<div class="control-group">
<div class="control-label">
<label>CSV/Excel File:</label>
</div>
<div class="controls">
<input type="file" name="file" id="file" class="input-large">
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" id="submit" name="Import" class="btn btn-primary button-loading" data-loading-text="Loading...">Upload</button>
</div>
</div>
</fieldset>
</form>
</div>
<div class="span3 hidden-phone"></div>
</div>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Subject</th>
<th>Description</th>
<th>Unit</th>
<th>Semester</th>
</tr>
</thead>
<?php
$SQLSELECT = "SELECT * FROM subject2 ";
$result_set = mysql_query($SQLSELECT, $conn);
while($row = mysql_fetch_array($result_set))
{
?>
<tr>
<td><?php echo $row['SUBJ_ID']; ?></td>
<td><?php echo $row['SUBJ_CODE']; ?></td>
<td><?php echo $row['SUBJ_DESCRIPTION']; ?></td>
<td><?php echo $row['UNIT']; ?></td>
<td><?php echo $row['SEMESTER']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</body>
</html>
import
Code (PHP)
<?php
include 'db.php';
if(isset($_POST["Import"])){
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
//It wiil insert a row to our subject table from our csv file`
$sql = "INSERT into subject2 (`SUBJ_CODE`, `SUBJ_DESCRIPTION`, `UNIT`, `PRE_REQUISITE`,COURSE_ID, `AY`, `SEMESTER`)
values('$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','$emapData[5]','$emapData[6]','$emapData[7]')";
//we are using mysql_query function. it returns a resource on true else False on error
$result = mysql_query( $sql, $conn );
// mysql_query("set character set utf8_general_ci");
if(! $result )
{
echo "<script type=\"text/javascript\">
alert(\"Invalid File:Please Upload CSV File.\");
window.location = \"index.php\"
</script>";
}
}
fclose($file);
//throws a message if data successfully imported to mysql database from excel file
echo "<script type=\"text/javascript\">
alert(\"CSV File has been successfully Imported.\");
window.location = \"index.php\"
</script>";
//close of connection
mysql_close($conn);
}
}
?>
Tag : PHP
Date :
2015-06-03 15:16:07
By :
se7enday007
View :
1183
Reply :
2
ลองดูพวก iconv() เพิ่มเติมครับ
Date :
2015-06-03 15:56:36
By :
mr.win
Date :
2015-06-03 16:15:37
By :
se7enday007
Load balance : Server 03