我从服务器中导出数据存为1.sql,这里面数据显示正常,因为有好几M。不能直接导入。我是用了下面一段代码来实现数据导入的,数据导入后中文全变成“?”,在mysql中直接插入一条带中文的数据可以正常显示,
<?php
$file_name="1.sql"; //要导入的SQL文件名
$dbhost="localhost"; //数据库主机名
$dbuser="root"; //数据库用户名
$dbpass="123456"; //数据库密码
$dbname="base"; //数据库名
set_time_limit(0); //设置超时时间为0,表示一直执行。当php在safe mode模式下无效,此时可能会导致导入超时,此时需要分段导入
$fp = @fopen($file_name, "r") or die("不能打开SQL文件 $file_name");//打开文件
mysql_connect($dbhost, $dbuser, $dbpass) or die("不能连接数据库 $dbhost");//连接数据库
mysql_select_db($dbname) or die ("不能打开数据库 $dbname");//打开数据库
echo "正在执行导入操作<span><br>";
while($SQL=GetNextSQL()){
 if (!mysql_query($SQL)){
 echo "<font color=red>执行出错:".mysql_error()."</font><br>";
 echo "SQL语句为:<br>".$SQL."<br>";
 };
}
echo "导入完成</span>";fclose($fp) or die("Can't close file $file_name");//关闭文件
mysql_close();//从文件中逐条取SQL
function GetNextSQL() {
 global $fp;
 $sql="";
 while ($line = @fgets($fp, 40960)) {
 $line = trim($line);
 //以下三句在高版本php中不需要,在部分低版本中也许需要修改
 $line = str_replace("\\\\","\\",$line);
 $line = str_replace("\'","'",$line);
 $line = str_replace("\\r\\n",chr(13).chr(10),$line);
// $line = stripcslashes($line);
 if (strlen($line)>1) {
 if ($line[0]=="-" && $line[1]=="-") {
 continue;
 }
 }
 $sql.=$line.chr(13).chr(10);
 if (strlen($line)>0){
 if ($line[strlen($line)-1]==";"){
 break;
 }
 }
 }
 return $sql;
}
?> 请教如何解决中文问号的问题。谢谢!!!

解决方案 »

  1.   

    注意一下编码问题,1.sql编码和数据库编码保持一致,否则还得加点代码。
      

  2.   

    你导出的数据编码是什么?最好设置为utf-8的
      

  3.   

    我是进phpmyadmin导的。。那里导出的时候貌似没有选编码的中!!!!!!
      

  4.   

    注意原来的数据库编码是latin1还是utf-8,如果是latin1还是要先转码
      

  5.   

    服务器上的数据是utf8_general_ci.格式的。