作者kingmin (高手)
看板PHP
标题Re: [请益] 网页改UTF8 MYSQL改UTF8却乱码 (附图)
时间Sun Oct 10 10:46:20 2010
※ 引述《buganini (霸格尼尼)》之铭言:
: ※ 引述《kingmin (高手)》之铭言:
: : 这是我的phpMyAdmin画面
: : http://img543.imageshack.us/img543/5405/20101009120719.jpg
: : 这是我的资料表
: : http://img269.imageshack.us/img269/3784/20101009120743.jpg
: : 这是我的资料表里的结构
: : http://img704.imageshack.us/img704/3241/20101009120809.jpg
: : 我PHP已全部改成utf8并设定无BOM
: : PHP里面也有加入了mysql_query("SET NAMES 'utf8'");
: : 请问现在PHP从 因该是BIG5的 MYSQL里取出的资料会变成乱码
: 应
: 那怎麽charset/collation全都是utf-8 ?
: : 不过我测试
: : INSERT INTO `search` (`ip` ,`name` ,`time` ,`code` )VALUES ('127.0.0.1',
: : 'Guest' , 1286598087 , '中文测是' );
: 试
: : 从phpMyAdmin里看到的是中文
: : 所以显示出网页也是中文没问题
: : 请问如果我想把资料库改用UTF8
: (A)
: 如果你用phpmyadmin看资料库内容都正确的话
: 没意外的话应该本来就是罗
: : 并且用CurlRequest从外部更新资料库的资料
: : 可是资料库里中文都不见了 变成是空的
: : 如果PHP里面改成mysql_query("SET NAMES 'big5'");
: : 在从外部更新资料库的资料 在从phpMyAdmin里看到的就是中文
: 这样看起来是资料库存UTF-8, 网页用Big5
: : 不过PHP上看到的就变成乱码了???
: : 请问如何设定才能存入资料是BIG5 取出资料是UTF8
: 有这个必要吗? 为什麽还要用Big5 ?
: 除非你的资料库要跟别人的程式共用
: 如果是这样的话那之後再说
: 可是怎麽跟(A)那边的说法不同??
这是连结资料库
function DB($v_host, $v_database, $v_user, $v_password){
mysql_connect($v_host,$v_user,$v_password) or die("无法连结服务器");
mysql_query("SET NAMES 'utf8'");
if(!mysql_select_db($v_database)){mysql_query("CREATE DATABASE
`$v_database`");}
mysql_select_db($v_database) or die("无法打开打开数据库");
}
这CURL应该跟网路上找的道程式码的差不多
class CurlRequest{
private $ch;
public function init($params)
{ $this->ch = curl_init();
$proxy=trim($params['proxy']);
$user_agent1 = array('Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-TW;
rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8','Mozilla/4.0 (compatible; MSIE 8.0;
Windows NT 6.1; Win64; x64; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET
CLR 3.0.30729; Media Center PC 6.0)','Mozilla/4.0 (compatible; MSIE 8.0;
Windows NT 6.1; WOW64; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1; SV1) ; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET
CLR 3.0.30729; Media Center PC 6.0)','Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.0; .NET CLR 1.0.3705; .NET CLR 1.1.4322)','Mozilla/4.0
(compatible; MSIE 5.00; Windows 98)');
$rand=rand(0,4);
$user_agent = $user_agent1[$rand];
$header = array(
"Accept: */*",
"Accept-Language: ru-ru,ru;q=0.7,en-us;q=0.5,en;q=0.3",
"Accept-Charset: UTF-8,windows-1251;q=0.7,*;q=0.7",
"Keep-Alive: 300");
if (isset($params['host']) && $params['host']) $header[]="Host:
".$host;
if (isset($params['header']) && $params['header'])
$header[]=$params['header'];
@curl_setopt ( $this -> ch , CURLOPT_RETURNTRANSFER , 1 );
@curl_setopt ( $this -> ch , CURLOPT_VERBOSE , 1 );
@curl_setopt ( $this -> ch , CURLOPT_HEADER , 1 );
if ($params['method'] == "HEAD") @curl_setopt($this ->
ch,CURLOPT_NOBODY,1);
@curl_setopt ( $this -> ch, CURLOPT_FOLLOWLOCATION,
$params['location']);
@curl_setopt ( $this -> ch , CURLOPT_HTTPHEADER, $header );
if ($params['referer']) @curl_setopt ($this -> ch ,
CURLOPT_REFERER, $params['referer'] );
@curl_setopt ( $this -> ch , CURLOPT_USERAGENT, $user_agent);
if ($params['cookie']) @curl_setopt ($this -> ch , CURLOPT_COOKIE,
$params['cookie']);
if ( $params['method'] == "POST" ){
curl_setopt( $this -> ch, CURLOPT_POST, true );
curl_setopt( $this -> ch, CURLOPT_POSTFIELDS,
$params['post_fields'] );
}
if (!empty($proxy)){
curl_setopt( $this -> ch, CURLOPT_PROXY, $proxy);$proxy1=$proxy;}
@curl_setopt ( $this -> ch, CURLOPT_URL, $params['url']);
@curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYPEER, 0 );
@curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYHOST, 0 );
if (isset($params['login']) & isset($params['password']))
@curl_setopt($this -> ch ,
CURLOPT_USERPWD,$params['login'].':'.$params['password']);
@curl_setopt ( $this -> ch , CURLOPT_TIMEOUT, $params['timeout']);
}
public function exec()
{ global $debug1,$nn,$proxy,$proxy1;
$response = curl_exec($this->ch);
$error = curl_error($this->ch);
$result = array( 'header' => '',
'body' => '',
'curl_error' => '',
'http_code' => '',
'last_url' => '');
if ( $error != "" ){
$result['curl_error'] = $error;
return $result;
}
$header_size = curl_getinfo($this->ch,CURLINFO_HEADER_SIZE);
$result['header'] = substr($response, 0, $header_size);
$result['body'] = substr( $response, $header_size );
$result['http_code'] = curl_getinfo($this -> ch,CURLINFO_HTTP_CODE);
$result['last_url'] = curl_getinfo($this ->
ch,CURLINFO_EFFECTIVE_URL);
if($debug1){
echo
$result['http_code']."<br>".$result['header']."<br><br>".$result['body']."<br>";}
if(!empty($proxy1)){
if($result['http_code'] <> "200" and $result['http_code'] <>
"302"){updataproxy();}}
return $result;
}
}
mysql_query("SET NAMES 'utf8'");
我也用了这四种//UTF-8 utf8 big5 Big5
还是不行????
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.115.19.192