日期:2014-05-16  浏览次数:21475 次

MySQL+PHP浜х敓涔辩爜鍘熷洜---杞浇
MySQL+PHP浜х敓涔辩爜鍘熷洜锛?

鈼?MySQL鏁版嵁搴撻粯璁ょ殑缂栫爜鏄痷tf8,濡傛灉杩欑缂栫爜涓庝綘鐨凱HP缃戦〉涓嶄竴鑷?鍙兘灏变細閫犳垚MySQL涔辩爜锛?

鈼?MySQL涓垱寤鸿〃鏃朵細璁╀綘閫夋嫨涓€绉嶇紪鐮?濡傛灉杩欑缂栫爜涓庝綘鐨勭綉椤电紪鐮佷笉涓€鑷?涔熷彲鑳介€犳垚MySQL涔辩爜锛?

鈼?MySQL鍒涘缓琛ㄦ椂娣诲姞瀛楁鏄彲浠ラ€夋嫨缂栫爜鐨?濡傛灉杩欑缂栫爜涓庝綘鐨勭綉椤电紪鐮佷笉涓€鑷?涔熷彲鑳介€犳垚MySQL涔辩爜锛?

鈼?鐢ㄦ埛鎻愪氦椤甸潰鐨勭紪鐮佷笌鏄剧ず鏁版嵁鐨勯〉闈㈢紪鐮佷笉涓€鑷?灏辫偗瀹氫細閫犳垚PHP椤甸潰涔辩爜锛?

鈼?濡傜敤鎴疯緭鍏ヨ祫鏂欑殑椤甸潰鏄痓ig5鐮? 鏄剧ず鐢ㄦ埛杈撳叆鐨勯〉闈㈠嵈鏄痝b2312,杩欑100%浼氶€犳垚PHP椤甸潰涔辩爜锛?

鈼?PHP椤甸潰瀛楃闆嗕笉姝g‘锛?

鈼?PHP杩炴帴MySQL鏁版嵁搴撹鍙ユ寚瀹氱殑缂栫爜涓嶆纭€?

娉ㄦ剰锛?

寰堝浜洪兘鎬€鐤慚ySQL鐗堟湰涓嶄竴鑷翠細瀵艰嚧涔辩爜,鐩镐俊鐪嬩簡鏈鏄庝綘灏变笉浼氳繖鏍疯涓轰簡銆?

骞虫椂浣犲湪鏌愪簺缃戠珯鐪嬪埌鐨勬枃瀛楀彲鑳芥湁鍑犵缂栫爜, 濡備綘鐪嬪埌涓€涓箒浣撳瓧,瀹冩湁鍙兘鏄痓ig5缂栫爜,涔熸湁 鍙兘鏄痷tf-8缂栫爜鐨?鏇存湁鍙兘鏄痝b鐮佺殑,娌¢敊,涔熷氨鏄鏈夌畝浣撶紪鐮佺殑绻佷綋瀛?涔熸湁绻佷綋缂栫爜鐨勭畝浣撳瓧,涓€瀹氳浜嗚В杩欎竴鐐广€?

濡傛灉浣犳槸鍋氫竴涓畝浣撶紪鐮佺殑缃戦〉,缂栫爜瀹氫负GB2312,濡傛灉鏈夐娓拰鍙版咕鍦板尯鐨勮瀹㈡彁浜ょ箒浣撶殑淇℃伅,灏卞彲鑳戒細閫犳垚涔辩爜,瑙e喅鏂规硶锛?

灏嗙綉绔欑紪鐮佽涓?utf-8,杩欐牱鍙互鍏煎涓栫晫涓婃墍鏈夊瓧绗︺€?

濡傛灉缃戠珯宸茬粡杩愪綔浜嗗ソ涔?宸叉湁寰堝鏃ф暟鎹?涓嶈兘鍐嶆洿鏀圭畝浣撲腑鏂囩殑璁惧畾,閭d箞寤鸿灏嗛〉闈㈢殑缂栫爜璁句负 GBK, GBK涓嶨B2312鐨勫尯鍒氨鍦ㄤ簬:GBK鑳芥瘮GB2312鏄剧ず鏇村鐨勫瓧绗?瑕佹樉绀虹畝浣撶爜鐨勭箒浣撳瓧,灏卞彧鑳界敤GBK銆?

浣跨敤MySQL+PHP浜х敓涔辩爜鐨勫師鍥犻兘浜嗚В寰楀緢娓呮浜?閭d箞瑙e喅灏变笉鍥伴毦浜嗐€?

MySQL+PHP浜х敓涔辩爜鐨勮В鍐冲姙娉曪細

濡傛灉瀹夎MySQL鐨勭紪鐮佸凡涓嶈兘鏇存敼,寰堝鏈嬪弸鏄喘涔拌櫄鎷熶富鏈哄缓绔嬬綉绔?鏃犳潈鏇存敼MySQL鐨勫畨瑁呯紪鐮?杩欎竴鍏虫垜浠彲浠ヨ烦杩?鍥犱负鍙鍚庨潰鐨勬鑱氭纭?涓€鏍疯兘瑙e喅涔辩爜闂銆?

淇敼鏁版嵁搴撶紪鐮侊紝濡傛灉鏄暟鎹簱缂栫爜涓嶆纭?鍙互鍦≒HPmyadmin 鎵ц濡備笅鍛戒护锛?

ALTER DATABASE 'test' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
浠ヤ笂鍛戒护灏辨槸灏唗est鏁版嵁搴撶殑缂栫爜璁句负utf8銆?

淇敼琛ㄧ殑缂栫爜锛?

ALTER TABLE 'category' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
浠ヤ笂鍛戒护灏辨槸灏嗕竴涓〃category鐨勭紪鐮佹敼涓簎tf8銆?

淇敼瀛楁鐨勭紪鐮侊細

ALTER TABLE 'test' CHANGE 'dd' 'dd' VARCHAR( 45 ) CHARACTER
SET utf8 COLLATE utf8_bin NOT NULL
浠ヤ笂鍛戒护灏辨槸灏唗est琛ㄤ腑dd鐨勫瓧娈电紪鐮佹敼涓簎tf8銆?

濡傛灉鏄繖绉嶆儏鍐靛鏄撹В鍐筹紝鍙渶妫€鏌ヤ笅椤甸潰锛屼慨鏀规簮鏂囦欢鐨刢harset鍗冲彲銆?

杩欑鎯呭喌涔熸槸淇敼椤甸潰charset鍗冲彲銆?

鍦ㄨ繛鎺ユ暟鎹簱鐨勮鍙ヤ腑銆?

mysql_connect('localhost','user','password');
mysql_select_db('my_db');
mysql_query("set names utf8;");     //select 鏁版嵁搴撲箣鍚庡姞澶氳繖涓€鍙?
涓轰簡閬垮厤PHP椤甸潰涔辩爜鐨勫彂鐢燂紝PHP椤甸潰寮€濮嬬涓€鍙?

header("content-type:text/html; charset=utf-8");
//寮鸿鎸囧畾椤甸潰鐨勭紪鐮侊紝浠ラ伩鍏嶄贡鐮?
娉ㄦ剰锛氱収浠ヤ笂鏂规硶淇敼浠ュ悗鍙兘淇濊瘉浣犳柊鎻掑叆鐨勬暟鎹笉浼氫贡鐮?涓句釜渚?濡傛灉浣犵敤鎴峰凡鎻愪氦鐨勬暟鎹槸BIG5,浣犲嵈鎯抽€氳繃浠ヤ笂鏂规硶鏀逛负鍙互鍦℅B2312鐨勭綉椤垫纭樉绀烘槸涓嶅彲鑳界殑, 杩欑鏂囧瓧鍐呯爜鐨勫彉鎹㈠彧鑳介€氳繃鍙﹀啓绋嬪簭鏉ヨВ鍐炽€?