这个是从Excel导入mqsql的页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<?php$mysql_server_name='localhost';
$mysql_username='test';
$mysql_password='123456'; $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password); mysql_select_db("beta_poreal",$conn);$row = 1;
$handle = fopen("tbl_portaldata.csv","r");
while ($data = fgetcsv($handle, 1000, ",")) {
$sqlstr1 = "insert into tbl_portaldata (route,addresscard,address,checkdate,personnelcard,personnel,fettle,apparatus) values ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]')";
mysql_query($sqlstr1)or die (mysql_error());
}
fclose($handle);
echo "ooooookkkkkkk!";
?>
<body>
</body>
</html>//////////////////////////////////////////////////////////////////////////
这个是读取数据的页面<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>excel3</title>
</head><?php$mysql_server_name='localhost';
$mysql_username='test';
$mysql_password='123456'; $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password); //mysql_query("set names 'gbk'");
//mysql_query("set names 'utf-8'");
mysql_select_db("beta_poreal",$conn);$sql="select * from tbl_portaldata ";
$rest=mysql_query($sql);
echo $sql;
?>
<body>
<table width="798" border="1">
<tr>
<td width="83"><div align="center"> 路线<br /></div></td>
<td width="86"><div align="center">地址卡</div></td>
<td width="85"><div align="center">地址</div></td>
<td width="101"><div align="center">巡查时间</div></td>
<td width="133"><div align="center">人员卡</div></td>
<td width="84"><div align="center">人员</div></td>
<td width="96"><div align="center">状态</div></td>
<td width="78"><div align="center">仪器</div></td>
</tr>
<?php
while($row=mysql_fetch_array($rest))
{
?> <tr>
<td><div align="center"><? echo $row["route"] ?></div></td>
<td><div align="center"><? echo $row["addresscard"] ?></div></td>
<td><div align="center"><? echo $row["address"] ?></div></td>
<td><div align="center"><? echo $row["checkdate"] ?></div></td>
<td><div align="center"><? echo $row["personnelcard"] ?></div></td>
<td><div align="center"><? echo $row["personnel"] ?></div></td>
<td><div align="center"><? echo $row["fettle"] ?></div></td>
<td><div align="center"><? echo $row["apparatus"] ?></div></td>
</tr>
<?
}
?>
</table>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<?php$mysql_server_name='localhost';
$mysql_username='test';
$mysql_password='123456'; $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password); mysql_select_db("beta_poreal",$conn);$row = 1;
$handle = fopen("tbl_portaldata.csv","r");
while ($data = fgetcsv($handle, 1000, ",")) {
$sqlstr1 = "insert into tbl_portaldata (route,addresscard,address,checkdate,personnelcard,personnel,fettle,apparatus) values ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]')";
mysql_query($sqlstr1)or die (mysql_error());
}
fclose($handle);
echo "ooooookkkkkkk!";
?>
<body>
</body>
</html>//////////////////////////////////////////////////////////////////////////
这个是读取数据的页面<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>excel3</title>
</head><?php$mysql_server_name='localhost';
$mysql_username='test';
$mysql_password='123456'; $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password); //mysql_query("set names 'gbk'");
//mysql_query("set names 'utf-8'");
mysql_select_db("beta_poreal",$conn);$sql="select * from tbl_portaldata ";
$rest=mysql_query($sql);
echo $sql;
?>
<body>
<table width="798" border="1">
<tr>
<td width="83"><div align="center"> 路线<br /></div></td>
<td width="86"><div align="center">地址卡</div></td>
<td width="85"><div align="center">地址</div></td>
<td width="101"><div align="center">巡查时间</div></td>
<td width="133"><div align="center">人员卡</div></td>
<td width="84"><div align="center">人员</div></td>
<td width="96"><div align="center">状态</div></td>
<td width="78"><div align="center">仪器</div></td>
</tr>
<?php
while($row=mysql_fetch_array($rest))
{
?> <tr>
<td><div align="center"><? echo $row["route"] ?></div></td>
<td><div align="center"><? echo $row["addresscard"] ?></div></td>
<td><div align="center"><? echo $row["address"] ?></div></td>
<td><div align="center"><? echo $row["checkdate"] ?></div></td>
<td><div align="center"><? echo $row["personnelcard"] ?></div></td>
<td><div align="center"><? echo $row["personnel"] ?></div></td>
<td><div align="center"><? echo $row["fettle"] ?></div></td>
<td><div align="center"><? echo $row["apparatus"] ?></div></td>
</tr>
<?
}
?>
</table>
</body>
</html>
导入数据库时
mysql_query("set names 'utf8'");
此外建议最好将从excel读出的均转成utf-8,然后再存入数据库读出时同样 mysql_query("set names 'utf8'");
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
这样还不行,我就不知道原因了。
可是又有新问题了,就是导入的每列只要第一个字符是汉字的,插入数据库就会为空,读出来的也是空白,除了那列第一个字符不是汉字的就可以读出显示出来,真的晕死了,谁知是怎回事,多谢了!
看看这个:
http://www.oneways.cn/blog/post/69.html