之前我在一篇文章里面讲到把故人居网站升级为UTF8字符集,看过的也知道一些有关PHP和MySQL字符集的问题,通常情况下MySQL默认使用的字符集是latin1,如果我们的系统要使用utf8或者别的字符集,就需要对MySQL进行配置,并且在PHP程序中做一点处理,大致的方法如下:
1.修改my.ini (或者my.cnf)文件,在文件的[mysqld]中加入下面一行
character_set_server = utf8
由于MySQL的字符集和连接校对有分级,分别是操作系统级、数据库服务器级、数据库客户端级,这几个级别是从上往下包含的,我上面的那一行 character_set_server 参数实际上就是设置了全局的字符集,这样不管是MySQL数据存储、数据连接、结果返回都将是utf8,当然,如果您愿意,您也可以进行分别的设置,比如做如下设置
default-character-set = utf8
default-collation = utf8_general_ci
这样的设置可以设置一个默认字符集,在您的应用中如果没有特殊指定字符集,那将默认使用utf8作为字符集,并且使用utf8_general_ci作为连接校对的默认字符集;
简单的处理方法就是如我前面的那样,加入一行即可。
2.PHP程序中连接MySQL的时候,或者在执行mysql_query的时候,执行以下一行SQL语句
SET NAMES utf8
就是说在执行mysql_query之前,或者在完成mysql_connect()之后,执行 mysql_query(“SET NAMES utf8)
有了前面的两个处理,您就可以正常的进行utf8字符集的数据库操作,不管在页面显示还是数据库中,字符都不会出现乱码。
前面说到的方法是以前用到的常用方法,最近在MySQL的官方发现了这样一个参数 init_connect ,它的意思呢就是在MySQL启动的时候,自动执行init_connect 参数里面的SQL语句,支持用分号间隔的多个语句,于是我们可以把 SET NAMES utf8语句加到my.ini文件里面,在PHP程序里面就不用单独处理了,加入参数的方法很简单,就是在my.ini文件的 [mysqld] 段加入下面一行
init_connect = ‘SET NAMES utf8’
不过这个方法有一个需要注意的地方,那就是该参数对于连接数据库的用户是超级用户组的用户将被忽略,那么在PHP程序中用来连接MySQL数据库的用户不能是SUPER组的用户,比如root用户就肯定不行,这样是为了避免该参数导致数据库致命错误,而无法使用任何一个用户连接上修改该项配置,之前我搞了两天都没有发现这样的问题,害我郁闷了很久。。。
有关MySQL的配置文件中用到的参数可以参考官方的文档 : http://dev.mysql.com/doc/refman/4.1/en/server-system-variables.html
呵呵,让人又爱又恨的MySQL!
你好,我有一个问题想问一下,我用的是mysql5.0.24,用的是utf8,使用WP很正常,但用phpmyadmin和命令行操作mysql的是候显示的中文还是乱码~~
[Comment ID #3939 Will Be Quoted Here]
这个问题完全可以使用我说的方法搞定,你可以在my.cnf里面加入init_connect那一句,不过你需要重新安装一次wordPress才行。 出现你这样的原因是虽然你的WP设置了使用utf8,但是PHP在进行数据库连接的时候并没有使用utf8连接校验,这样插入到里面的数据存储方式虽然是utf8,但是数据本身并不是utf8的,PHP读出来的时候使用的方法和插入一样,所以WP里面看到的并不会是乱码,相反在更加聪明的phpmyadmin里面却看到了乱码,因为它使用了正确的utf8连接校验。
我按照你的方法试过了,可能我在windows系统上部署的PHP/MYSQL,所以没有my.cnf这个文件,但我把init_connect这句话放到my.ini文件里的时候一连接就报错。
[Comment ID #3972 Will Be Quoted Here]
windows也是没有问题的,我这里除了FreeBSD的机器上外,在我的笔记本上也安装了MySQL5,如果使用默认的安装,应该会安装在 C:\Program Files\MySQL\MySQL Server 5.0\ 也有可能您定制安装在别的目录,您可以通过下面的命令得知您的MySQL主目录 basedir 所在位置:
mysqladmin -uroot -p variables
查看其中的basedir项,MySQL从4.x开始在Windows上的配置文件my.ini默认存放在basedir下,以前是存放在c:\my.cnf下,所以您提到的只有my.ini是正常的,同时我们也正是要配置这个my.ini文件,不知道您修改my.ini文件的方法是怎样的,下面是我的my.ini的部分内容,您可以参考一下:
[client]
port=3306
default-character-set=utf8
[mysqld]
port=3306
basedir=”C:/Program Files/MySQL/MySQL Server 5.0/”
datadir=”C:/Program Files/MySQL/MySQL Server 5.0/Data/”
character_set_server = utf8
init_connect=’SET NAMES utf8′
default-storage-engine=INNODB
max_connections=100
…
我按照以上方法,将mysql server的character_set_server = utf8
init_connect=’SET NAMES utf8′
但重新安装mu的wordpress后,新增用户blog便不能使用刚新开的用户blog,出现404Page Not Found
请问如将server的character_set设了做UTF8后是否会有影响?
曾经尝试改回原来的设定character_set_server = utf8,重裝mu wordpress後,但问题仍旧
请问有何方法可以解决这个问题?
谢谢,测试一下,很好用
不过有时候那些参数在虚拟主机改不了的
[Comment ID #31773 Will Be Quoted Here]
如果是虚拟主机的话,的确有些麻烦,虚拟主机就只能照顾人家数据库的配置了,人家是什么你就用什么吧,不过你确保你连接的时候都是要utf-8,那存入和读取出来的数据都是统一的,只不过在数据库里面存储的可能是乱的,那也没什么关系。
Je lis souvent, comment vas-tu? Quel genre d’information! Ici, sur ce site, merci administrer cette page.
My developer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the costs.
But he’s tryiong none the less. I’ve been using WordPress on numerous websites for about a year and am
worried about switching to another platform. I have
heard fantastic things about blogengine.net. Is there
a way I can import all my wordpress posts into it?
Any help would be really appreciated!
Hello, i think that i saw you visited my website so i came to “return the favor”.I am trying
to find things to enhance my website!I suppose its ok to use some of your ideas!!
It is perfect time to make some plans for the longer term and it’s time
to be happy. I have learn this publish and if I could I desire to counsel you some interesting things or
tips. Perhaps you could write subsequent articles regarding this article.
I wish to learn even more issues approximately it!
Hi everybody, here every person is sharing such knowledge, thus
it’s good to read this weblog, and I used to pay a quick visit this weblog every day.
Superb, what a weblog it is! This weblog provides helpful data to us, keep it up.
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
You clearly know what youre talking about, why throw away your intelligence on just posting videos to your weblog when you could be giving us something enlightening to read?
I think this is among the most vital info for me. And i’m glad
reading your article. But wanna remark on some general things, The
web site style is wonderful, the articles is really great : D.
Good job, cheers
I am extremely impressed with your writing skills as well as with the layout
on your weblog. Is this a paid theme or did you customize it
yourself? Either way keep up the nice quality writing, it’s rare to see
a nice blog like this one these days.
Have you ever thought about including a little bit more than just your articles?
I mean, what you say is fundamental and everything.
Nevertheless think of if you added some great graphics or videos to give your posts more,
“pop”! Your content is excellent but with images and clips,
this blog could definitely be one of the very best
in its niche. Fantastic blog!
Descargar facebook http://twitter.com/descargar_hq descargar facebook
Hi there everyone, it’s my first visit at this web site, and article is in fact fruitful in support of me, keep up
posting these types of articles. Descargar facebook http://twitter.com/descargar_hq descargar facebook
Wow, marvelous weblog layout! How long have you ever been blogging for?
you made blogging look easy. The overall glance of
your website is magnificent, as neatly as the content!
I really like it when folks come together and share ideas.
Great website, stick with it!
When someone writes an piece of writing he/she
maintains the thought of a user in his/her mind that how a user can understand it.
Therefore that’s why this piece of writing is outstdanding.
Thanks!
Hello very cool website!! Man .. Beautiful .. Amazing ..
I will bookmark your website and take the feeds also?
I’m glad to search out so many useful information here within the put up, we want work out more strategies in this regard, thanks for sharing.
. . . . .
Asking questions are in fact nice thing if you are not understanding anything entirely, except this piece of writing gives good understanding yet.
I think this is one of the most significant information for
me. And i’m glad reading your article. But wanna remark on few
general things, The website style is perfect, the
articles is really great : D. Good job, cheers
I really like your blog.. very nice colors & theme.
Did you make this website yourself or did you hire someone to do it for you?
Plz respond as I’m looking to construct my own blog and would like to find out where u got this from.
kudos
Heya i’m for the first time here. I came across this board and I to find
It really helpful & it helped me out much. I hope to
provide one thing back and aid others like you helped
me.
What i don’t realize is actually how you’re now not actually
much more well-preferred than you may be right now.
You are very intelligent. You already know therefore considerably relating to this matter, produced
me personally consider it from a lot of various angles.
Its like women and men are not interested unless it’s one thing to do with Lady gaga!
Your personal stuffs great. At all times care for it up!
My family members always say that I am wasting my time here
at web, except I know I am getting familiarity everyday by reading
such fastidious posts.
If you would like to grow your know-how only keep visiting
this site and be updated with the most recent information posted here.
I know this site gives quality based articles
or reviews and other stuff, is there any other site which offers such data in quality?
Fine way of telling, and fastidious piece of writing to obtain facts about my presentation subject, which i am going to deliver in college.
you are in reality a excellent webmaster. The web site
loading pace is amazing. It sort of feels that you’re doing any distinctive trick.
Also, The contents are masterpiece. you’ve done a
magnificent activity on this subject!
Excellent goods from you, man. I’ve take note your stuff prior to and you are just too
great. I really like what you have acquired here, certainly like what you’re saying
and the best way by which you say it. You’re making it enjoyable and you
continue to take care of to stay it wise. I can’t wait to learn much
more from you. That is really a tremendous site.
Hurrah! At last I got a webpage from where I be able to genuinely
obtain useful information regarding my study and
knowledge.
Thanks designed for sharing such a nice opinion, post is fastidious, thats why i have read it entirely
Definitely believe that which you said. Your favorite reason seemed to be on the net the easiest thing
to be aware of. I say to you, I certainly get annoyed while
people consider worries that they plainly do not know about.
You managed to hit the nail upon the top and defined out
the whole thing without having side effect ,
people can take a signal. Will probably be back to get more.
Thanks
Oh my goodness! Awesome article dude! Many thanks, However I am experiencing troubles with your RSS.
I don’t know the reason why I am unable to subscribe to it.
Is there anybody getting identical RSS problems?
Anyone who knows the solution can you kindly respond? Thanks!!
Its like you read my mind! You seem to grasp a lot about this,
like you wrote the e book in it or something. I think that you just could do with some percent
to drive the message house a bit, but instead of that,
this is excellent blog. An excellent read. I’ll certainly be back.
Thanks , I’ve just been looking for information approximately this topic for a while and yours is the greatest
I have discovered so far. However, what about the bottom line?
Are you certain concerning the source?
Hola! I’ve been reading your site for a long time now and finally got
the bravery to go ahead and give you a shout out
from Kingwood Tx! Just wanted to mention keep up
the great job!
Hi, I do think this is an excellent web site. I stumbledupon it 😉
I am going to come back once again since I saved as a favorite it.
Money and freedom is the best way to change, may you be rich and continue to help
other people.
Oh my goodness! Awesome article dude! Many thanks, However I am having troubles with
your RSS. I don’t know why I am unable to join it.
Is there anyone else getting similar RSS problems? Anybody who knows the solution will you kindly respond?
Thanks!!
I always spent my half an hour to read this web site’s articles all the time along
with a mug of coffee.
Excellent post. I used to be checking constantly this weblog and I’m inspired!
Very helpful info particularly the final phase 🙂 I handle such information much.
I was looking for this certain info for a long time. Thank you and best of luck.
I love it whenever people come together and
share opinions. Great blog, continue the good work!
Asking questions are genuinely pleasant thing if you are not understanding something fully, but this post offers good understanding yet.
If you would like to obtain much from this post then you have to apply such strategies to your won web site.
plenty of fish natalielise
I’m not that much of a internet reader to be honest but your blogs really nice, keep it up!
I’ll go ahead and bookmark your site to come back in the future.
Many thanks
This paragraph is in fact a nice one it helps new net visitors, who are wishing in favor of blogging.
Thanks a lot. Lots of posts!
discount pharmacy online
canadian pharmaceuticals online
canadapharmacyonline.com
canadian viagra
canadian online pharmacies – https://www.canadianpharmacyu.com/
us pharmacy no prior prescription Jen c00e7c3
I blog often and I really appreciate your content.
Your article has really peaked my interest. I’m going to take a
note of your blog and keep checking for new details about once per week.
I subscribed to your RSS feed as well.
What’s up everyone, it’s my first go to see at this web
page, and article is truly fruitful for me, keep up posting these articles.
Hi to every one, because I am in fact keen of reading this
webpage’s post to be updated regularly. It consists of pleasant material.
Hello! I simply would like to give you a huge thumbs up for the great information you’ve got here on this post.
I am returning to your website for more soon.
wonderful issues altogether, you just received a logo new reader.
What may you recommend in regards to your submit that you simply made a few days in the past?
Any certain?
What’s up to all, how is all, I think every one is getting more from this website,
and your views are fastidious in favor of new people.
Truly a lot of valuable material!
viagra grapefruit
http://viaonlinebuymsn.com/
cheap viagra costco
dicloxacil viagra
Thanks for the good writeup. It if truth be told used to be a entertainment account it.
Look complex to far delivered agreeable from you! However,
how can we keep in touch?
Informative article, totally what I was looking for.
Thanks in favor of sharing such a fastidious thought, piece of writing is
nice, thats why i have read it entirely
This post is priceless. When can I find out more?
Peculiar article, totally what I needed.
Does your blog have a contact page? I’m having a
tough time locating it but, I’d like to send you
an email. I’ve got some ideas for your blog you might be interested in hearing.
Either way, great blog and I look forward to seeing it improve over time.
You can certainly see your enthusiasm in the article you write.
The sector hopes for even more passionate writers like
you who are not afraid to mention how they believe.
Always follow your heart.
Fantastic beat ! I would like to apprentice while you amend your website,
how could i subscribe for a weblog website?
The account helped me a acceptable deal. I were tiny bit familiar of this
your broadcast offered brilliant transparent concept
Great blog here! Also your website loads up
very fast! What web host are you using? Can I get your affiliate link to your host?
I wish my website loaded up as quickly as yours
lol
Its like you read my thoughts! You appear to understand so much approximately this, such as you wrote the
e book in it or something. I believe that you simply could do with
a few % to pressure the message house a bit, but other than that, this is great blog.
An excellent read. I will definitely be back.
Just desire to say your article is as amazing. The clearness to your post is simply nice and that i could suppose you are knowledgeable in this subject.
Well with your permission allow me to snatch your RSS feed to keep
updated with imminent post. Thanks a million and please continue the enjoyable work.
Great post. I was checking continuously this blog and I am impressed!
Very helpful information particularly the ultimate
phase 🙂 I deal with such information a lot. I used to be seeking this particular info for a long
time. Thank you and best of luck.
Hi there! I’m at work browsing your blog from my new apple iphone!
Just wanted to say I love reading your blog and look forward to all your posts!
Keep up the excellent work!
Does your website have a contact page? I’m having trouble locating it
but, I’d like to send you an email. I’ve got some recommendations for your blog
you might be interested in hearing. Either way, great
site and I look forward to seeing it improve over time.
I was recommended this blog by my cousin. I am not sure whether this post
is written by him as nobody else know such detailed about my trouble.
You are amazing! Thanks!
Aw, this was a very nice post. Taking the time and actual effort
to produce a very good article… but what can I say… I procrastinate a lot and never manage to get nearly anything
done.