php连接两个数据库查询导入

2015-12-31 浏览:1276
php连接两个数据库查询导入
评论:(0)复制地址

<?php 

header("content-type:text/html; charset=utf-8");

$host="localhost"; 

$user="root"; 

$password="root"; 

$database_1="导入数据库"; 

$database_2="原始数据库"; 


session_start(); 


//php获取中文字符拼音首字母 

function getFirstCharter($str){ 

 if(empty($str)){return '';} 

 $fchar=ord($str{0}); 

 if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0}); 

 $s1=iconv('UTF-8','gb2312',$str); 

 $s2=iconv('gb2312','UTF-8',$s1); 

 $s=$s2==$str?$s1:$str; 

 $asc=ord($s{0})*256+ord($s{1})-65536; 

 if($asc>=-20319&&$asc<=-20284) return 'A'; 

 if($asc>=-20283&&$asc<=-19776) return 'B'; 

 if($asc>=-19775&&$asc<=-19219) return 'C'; 

 if($asc>=-19218&&$asc<=-18711) return 'D'; 

 if($asc>=-18710&&$asc<=-18527) return 'E'; 

 if($asc>=-18526&&$asc<=-18240) return 'F'; 

 if($asc>=-18239&&$asc<=-17923) return 'G'; 

 if($asc>=-17922&&$asc<=-17418) return 'H'; 

 if($asc>=-17417&&$asc<=-16475) return 'J'; 

 if($asc>=-16474&&$asc<=-16213) return 'K'; 

 if($asc>=-16212&&$asc<=-15641) return 'L'; 

 if($asc>=-15640&&$asc<=-15166) return 'M'; 

 if($asc>=-15165&&$asc<=-14923) return 'N'; 

 if($asc>=-14922&&$asc<=-14915) return 'O'; 

 if($asc>=-14914&&$asc<=-14631) return 'P'; 

 if($asc>=-14630&&$asc<=-14150) return 'Q'; 

 if($asc>=-14149&&$asc<=-14091) return 'R'; 

 if($asc>=-14090&&$asc<=-13319) return 'S'; 

 if($asc>=-13318&&$asc<=-12839) return 'T'; 

 if($asc>=-12838&&$asc<=-12557) return 'W'; 

 if($asc>=-12556&&$asc<=-11848) return 'X'; 

 if($asc>=-11847&&$asc<=-11056) return 'Y'; 

 if($asc>=-11055&&$asc<=-10247) return 'Z'; 

 return $str; 

}


$link=mysql_connect($host,$user,$password) or die("fail"); 

mysql_select_db($database_2,$link); 

mysql_query("set names 'utf8'");//使用utf8连接数据库

$sql_1="select a.id,a.catid,a.title,a.keywords,a.description,b.content from news as a left join news_data as b on b.id=a.id where a.catid=17";

$result_data2=mysql_query($sql_1); 


while(list($id['id'],$catid['catid'],$title['title'],$keywords['keywords'],$description['description'],$content['content'])=mysql_fetch_array($result_data2)){ 

echo $id['id'].' '.$catid['catid'].' '.$title['title'].' '.$keywords['keywords'].' '.$description['description'].' <br>'.$content['content'].' <br><hr>'; 


$row_cid=18; 

$row_title=$title['title']; 

$row_tit=mb_substr($row_title,0,1,"utf-8");

$row_letter=getFirstCharter($row_tit);

$row_keywords=$keywords['keywords']; 

$row_description=$description['description']; 

$row_content=$content['content'];

$row_author='213';

$row_authorid=1;

$row_time=time();

$row_lastedit=time();

$row_lasteditor='213';

$row_lasteditorid=1;


mysql_select_db($database_1,$link); 

$sql_2="insert into wiki_doc(letter,title,tag,summary,content,author,authorid,time,lastedit,lasteditor,lasteditorid) 

   values('$row_letter','$row_title','$row_keywords','$row_description','$row_content','$row_author','$row_authorid','$row_time','$row_lastedit','$row_lasteditor','$row_lasteditorid')"; 

mysql_query($sql_2); 

$did = mysql_insert_id(); //获取自动生成的ID


$sql_3="insert into wiki_categorylink(did,cid) 

   values('$did','$row_cid')"; 

mysql_query($sql_3); 



?>


评论:(0)复制地址
发布:苗景云 | 分类:IT技术&设计 | Tags:

相关文章

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。