php读mysqlä¸æä¹±ç é®é¢è§£å³æ¹æ³
1ãæ件ç¼ç ï¼æçæ¯é¡µé¢æ件(.html,.phpç)æ¬èº«æ¯ä»¥ä½ç§ç¼ç æ¥ä¿åçãè®°äºæ¬åDreamweaverå¨æå¼é¡µé¢æ¶åä¼èªå¨è¯å«æ件ç¼ç å èä¸å¤ªä¼åºé®é¢ãèZendStudioå´ä¸ä¼èªå¨è¯å«ç¼ç ï¼å®åªä¼æ ¹æ®é¦é项çé ç½®åºå®ä»¥æç§ç¼ç æå¼æ件ï¼å¦æå·¥ä½æ¶åä¸ä¸æ³¨æï¼ç¨
1ãæ件ç¼ç ï¼æçæ¯é¡µé¢æ件(.html,.phpç)æ¬èº«æ¯ä»¥ä½ç§ç¼ç æ¥ä¿åçãè®°äºæ¬åDreamweaverå¨æå¼é¡µé¢æ¶åä¼èªå¨è¯å«æ件ç¼ç å èä¸å¤ªä¼åºé®é¢ãèZendStudioå´ä¸ä¼èªå¨è¯å«ç¼ç ï¼å®åªä¼æ ¹æ®é¦é项çé ç½®åºå®ä»¥æç§ç¼ç æå¼æ件ï¼å¦æå·¥ä½æ¶åä¸ä¸æ³¨æï¼ç¨é误ç¼ç æå¼æ件ï¼åäºä¿®æ¹ä¹åä¸ä¿åï¼ä¹±ç å°±åºç°äºã
2ã页é¢ç³æç¼ç ï¼å¨HTML代ç HEADéé¢ï¼å¯ä»¥ç¨ï¼è¿å¥ä¸å®è¦åå¨
3ãæ°æ®åºè¿æ¥ç¼ç ï¼æçæ¯è¿è¡æ°æ®åºæä½æ¶å以åªç§ç¼ç ä¸æ°æ®åºä¼ è¾æ°æ®ï¼è¿ééè¦æ³¨æçæ¯ä¸è¦ä¸æ°æ®åºæ¬èº«çç¼ç æ··æ·ï¼æ¯å¦MySQLå é¨é»è®¤æ¯latin1ç¼ç ï¼ä¹å°±æ¯è¯´Mysqlæ¯ä»¥latin1ç¼ç æ¥åå¨æ°æ®ï¼ä»¥å ¶ä»ç¼ç ä¼ è¾ç»Mysqlçæ°æ®ä¼è¢«è½¬æ¢ælatin1ç¼ç ã
ç¥éäºWEBå¼åä¸åªäºå°æ¹æ¶åå°äºç¼ç ï¼ä¹å°±ç¥éäºä¹±ç 产ççåå ï¼ä¸è¿°3项ç¼ç 设置ä¸ä¸è´ï¼ç±äºåç§ç¼ç ç»å¤§é¨åæ¯å ¼å®¹ASCIIçï¼æ以è±æ符å·ä¸ä¼åºç°ï¼ä¸æå°±åéäºãä¸é¢æ¯ä¸äºå¸¸è§çé误æ åµä¸è§£å³ï¼
1ãæ°æ®åºéç¨UTF8ç¼ç ï¼è页é¢ç³æç¼ç æ¯GB2312ï¼è¿æ¯æ常è§ç产çä¹±ç çåå ãè¿æ¶åå¨PHPèæ¬éé¢ç´æ¥SELECTæ°æ®åºæ¥çå°±æ¯ä¹±ç ï¼éè¦å¨æ¥è¯¢åå 使ç¨ï¼
mysql_query("SET NAMES GBK");
æmysql_query("SET NAMES GB2312");
æ¥è®¾å®MYSQLè¿æ¥ç¼ç ï¼ä¿è¯é¡µé¢ç³æç¼ç ä¸è¿é设å®çè¿æ¥ç¼ç ä¸è´ï¼GBKæ¯GB2312çæ©å±ï¼ãå¦æ页é¢æ¯UTF-8ç¼ç çè¯ï¼å¯ä»¥ç¨ï¼
mysql_query("SET NAMES UTF8");
注ææ¯UTF8èä¸æ¯ä¸è¬ç¨çUTF-8ãåå¦é¡µé¢ç³æçç¼ç ä¸æ°æ®åºå
é¨ç¼ç ä¸è´å¯ä»¥ä¸è®¾å®è¿æ¥ç¼ç ã
注ï¼äºå®ä¸MYSQLçæ°æ®è¾å
¥è¾åºæ¯ä¸é¢è®²çæ´å¤æä¸äºï¼MYSQLé
ç½®æ件my.iniä¸å®ä¹äº2个é»è®¤ç¼ç ï¼åå«æ¯[client]éçdefault-character-setå[mysqld]éçdefault-character-setæ¥åå«è®¾å®é»è®¤æ¶å客æ·ç«¯è¿æ¥åæ°æ®åºå
é¨æéç¨çç¼ç ãæ们ä¸é¢æå®çç¼ç å
¶å®æ¯MYSQL客æ·ç«¯è¿æ¥æå¡å¨æ¶åçå½ä»¤è¡åæ°character_set_clientï¼æ¥åè¯MYSQLæå¡å¨æ¥åå°ç客æ·ç«¯æ°æ®æ¯ä»ä¹ç¼ç çï¼èä¸æ¯éç¨é»è®¤ç¼ç ã
2ã页é¢ç³æç¼ç ä¸æ件æ¬èº«ç¼ç ä¸ä¸è´ï¼è¿ç§æ åµå¾å°åçï¼å 为å¦æç¼ç ä¸ä¸è´ç¾å·¥å页é¢æ¶åå¨æµè§å¨çå°çå°±æ¯ä¹±ç äºãæ´å¤æ¶åæ¯åå¸ä»¥åä¿®æ¹ä¸äºå°BUGï¼ä»¥é误ç¼ç æå¼é¡µé¢ç¶åä¿å导è´çãæè æ¯ç¨æäºFTP软件ç´æ¥å¨çº¿ä¿®æ¹æ件ï¼æ¯å¦CuteFTPï¼ç±äºè½¯ä»¶ç¼ç é ç½®é误è导è´è½¬æ¢éäºç¼ç ã
3ãä¸äºç§ç¨èæ主æºçæåï¼ææä¸è¿°3项ç¼ç é½è®¾ç½®æ£ç¡®äºè¿æ¯æä¹±ç ãæ¯æ¹è¯´ç½é¡µæ¯GB2312ç¼ç çï¼IEçæµè§å¨æå¼å´æ»æ¯è¯å«æUTF-8ï¼ç½é¡µHEADéé¢å·²ç»ç³ææ¯GB2312äºï¼æå¨ä¿®æ¹æµè§å¨ç¼ç 为GB2312å页é¢æ¾ç¤ºæ£å¸¸ã产çåå æ¯æå¡å¨Apache设å®äºæå¡å¨å ¨å±çé»è®¤ç¼ç ï¼å¨httpd.conféé¢å äºAddDefaultCharset UTF-8ãè¿æ¶åæå¡å¨ä¼é¦å åéHTTP头ç»æµè§å¨ï¼å ¶ä¼å 级æ¯é¡µé¢éç³æç¼ç é«ï¼èªç¶æµè§å¨å°±è¯å«éäºã解å³åæ³æ2个ï¼è¯·ç®¡çåå¨é ç½®æ件èªå·±çèæºéå ä¸ä¸æ¡AddDefaultCharset GB2312æ¥è¦çå ¨å±é ç½®ï¼æè å¨èªå·±ç®å½ç.htaccesséé ç½®ã
ä¹±ç 解å³æ¹æ³
è¦è§£å³ä¹±ç é®é¢ï¼é¦å
å¿
é¡»å¼æ¸
æ¥èªå·±æ°æ®åºç¨ä»ä¹ç¼ç ãå¦æ没æææï¼å°æ¯é»è®¤çlatin1ã
æ们ç¨å¾æå¤çåºè¯¥æ¯è¿3ç§å符é gb2312ï¼gbkï¼utf8ã
é£ä¹æ们å¦ä½å»æå®æ°æ®åºçå符éå¢ï¼ä¸é¢ä¹gbk为ä¾
ãå¨MySQL Command Line Clientå建æ°æ®åºã
mysql> CREATE TABLE `mysqlcode` (
-> `id` TINYINT( 255 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
-> `content` VARCHAR( 255 ) NOT NULL
-> ) TYPE = MYISAM CHARACTER SET gbk COLLATE gbk_chinese_ci;
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> desc mysqlcode;
+---------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-----------------------+------+-----+---------+----------------+
| id | tinyint(255) unsigned | NO | PRI | | auto_increment |
| content | varchar(255) | NO | | | |
+---------+-----------------------+------+-----+---------+----------------+
2 rows in set (0.02 sec)
å
¶ä¸åé¢çTYPE = MYISAM CHARACTER SET gbk COLLATE gbk_chinese_ci;
å°±æ¯æå®æ°æ®åºçå符éï¼COLLATE (æ ¡å)ï¼è®©mysqlåæ¶æ¯æå¤ç§ç¼ç çæ°æ®åºã
å½ç¶æ们ä¹å¯ä»¥éè¿å¦ä¸æ令修æ¹æ°æ®åºçå符é
alter database da_name default character set 'charset'.
客æ·ç«¯ä»¥ gbk格å¼åé ï¼å¯ä»¥éç¨ä¸è¿°é
ç½®:
SET character_set_client='gbk'
SET character_set_connection='gbk'
SET character_set_results='gbk'
è¿ä¸ªé
置就çä»·äº SET NAMES 'gbk'ã
ç°å¨å¯¹åæå建çæ°æ®åºæä½
mysql> use test;
Database changed
mysql> insert into mysqlcode values(null,'phpç±å¥½è
');
ERROR 1406 (22001): Data too long for column 'content' at row 1
没ææå®å符é为gbkï¼æå
¥æ¶åºé
mysql> set names 'gbk';
Query OK, 0 rows affected (0.02 sec)
æå®å符é为 gbk
mysql> insert into mysqlcode values(null,'phpç±å¥½è
');
Query OK, 1 row affected (0.00 sec)
æå
¥æå
mysql> select * from mysqlcode;
+----+-----------+
| id | content |
+----+-----------+
| 1 | phpç±å¥½ç |
+----+-----------+
1 row in set (0.00 sec)
å¨æ²¡ææå®å符égbkæ¶è¯»åä¹ä¼åºç°ä¹±ç ï¼å¦ä¸
mysql> select * from mysqlcode;
+----+---------+
| id | content |
+----+---------+
| 1 | php??? |
+----+---------+
1 row in set (0.00 sec)
ãå¨phpmyadminå建æ°æ®åº,并æå®å符éã
表类åæ ¹æ®èªå·±éè¦éï¼è¿ééMyISAM(æ¯æå
¨ææ£ç´¢);
æ´çéæ© gbk_chinese_ci ä¹å°±æ¯gbkå符é
gbk_bin ç®ä½ä¸æ, äºè¿å¶ãgbk_chinese_ci ç®ä½ä¸æ, ä¸åºå大å°åã
å¨åæå建çæ°æ®åºæå
¥æ°æ®åº
åæµè§æ¶åç°æ¯ä¹±ç
为ä»ä¹å¢ï¼æ¯å 为æ°æ®åºä¸ºgbkå符éï¼èæ们æä½æ¶æ²¡ææå®ä¸ºgbk
åå°æ°æ®åºé¦é¡µ
å¯ä»¥çå° mysql è¿æ¥æ ¡å¯¹é»è®¤çlatin1_binãæ们å°å
¶æ¹ä¸ºgbk_chinese_ci
åæå
¥ä¸æ¡æ°æ®ãçï¼è¿æ¡å·²ç»æ£å¸¸äº
ã解å³php读åæ°æ®åºä¹±ç ã
ä»ä»¥æ°æ®åºmysqlcode为ä¾
$conn = mysql_connect("localhost","root","89973645");
mysql_query("set names 'gbk'");//è¿å°±æ¯æå®æ°æ®åºå符éï¼ä¸è¬æ¾å¨è¿æ¥æ°æ®åºåé¢å°±ç³»äº
mysql_select_db("test");
$sql = "select * from mysqlcode";
$result = mysql_query($sql,$conn);
?>
echo " }?>
id
å
容
";".$row['id']."
".$row['content']."