本教程講如何用 PHP+MySQL 搭建網(wǎng)站。
之前一直有想過(guò)搭建自己的個(gè)人網(wǎng)站,然后上周通過(guò)阿里云申請(qǐng)的域名和空間都通過(guò)審核了,于是就開(kāi)始研究如何用 PHP+MYSQL 搭建網(wǎng)站,研究了差不多兩周,總算搞定了一個(gè)小型的 blog 類的網(wǎng)站。當(dāng)然,整個(gè)過(guò)程是通過(guò)學(xué)習(xí)《PHP and MySQL web development》下來(lái)的,這篇教程中的例子是在原書中的 Chapter 28: Building a Content Manager System 章節(jié)的基礎(chǔ)上,修改完成的。所有的源代碼可以在這里找到,本來(lái)想買中文版的書來(lái)看,但是看評(píng)論說(shuō)翻譯的不行,然后就直接看的原版第 3 版的英文 PDF,有想要的同學(xué)可以自行 Google 或者給我留言。
我是從 0 開(kāi)始學(xué)習(xí)PHP+MySQL的,基本上網(wǎng)站搭建下來(lái)花了不到兩周;當(dāng)然,這里需要你有一定的 HTML 和 SQL 的基礎(chǔ),很基本的就行了。PHP 其實(shí)就是個(gè)腳本語(yǔ)言,在原來(lái)的 HTML 中,把需要?jiǎng)討B(tài)計(jì)算的部分用 PHP 完成,其實(shí)最后輸出出來(lái)的還是 HTML 的格式。
所以不用覺(jué)得很困難,當(dāng)然我在學(xué)習(xí)的過(guò)程中還是經(jīng)歷的很多痛苦的,因?yàn)闀举Y料比較久遠(yuǎn),而且阿里的云主機(jī)也不知道怎么連數(shù)據(jù)庫(kù),所以很有必要記錄下來(lái)。
按照慣例,我們來(lái)看下需要完成哪些準(zhǔn)備工作吧。
這個(gè)我是通過(guò)阿里云注冊(cè)的(www.net.cn)里面有域名申請(qǐng),我申請(qǐng)了一個(gè).cn
的,一年 29 塊錢。域名申請(qǐng)很簡(jiǎn)單,掏錢就可以了。主要是之后還要申請(qǐng)主機(jī),這樣可以把你的域名解析成你的主機(jī) ip 地址,然后用戶訪問(wèn)你的域名以后就能跳轉(zhuǎn)到你的主機(jī)上去,找到相應(yīng)的網(wǎng)頁(yè)文件了。
整個(gè)主機(jī)申請(qǐng)的過(guò)程可以參照網(wǎng)站上面的介紹,里面有特別詳細(xì)的過(guò)程,包括填寫資料,上傳省份證、到指定地點(diǎn)拍照、郵寄資料,然后就等待審核吧。我之前的所有步驟,就是拍照、郵寄資料這些的都是兩天就搞定了,但是最后資料審核特別慢,基本上兩周才審核通過(guò)。你也可以申請(qǐng)國(guó)外的主機(jī),好像不用審核,但是需要收費(fèi)。國(guó)內(nèi)阿里會(huì)免費(fèi)提供你一個(gè)主機(jī),但是就是需要審核。
這個(gè)是我在阿里的主機(jī)界面,在下面的新手必讀里面,寫的比較清楚,按照這個(gè)步驟來(lái),你的主機(jī)申請(qǐng)是沒(méi)啥問(wèn)題的。
這個(gè)步驟我研究了好久才弄明白在哪里操作。本來(lái)按照上圖里面的 3 步驟來(lái),本地下了個(gè) Mysql 然后無(wú)論如何也連接不到遠(yuǎn)程去。后來(lái)無(wú)意間發(fā)現(xiàn)可以在網(wǎng)頁(yè)上管理數(shù)據(jù)庫(kù)。
就是上圖里面的,在你的主機(jī)目錄后面,點(diǎn)擊“管理”,然后就跳轉(zhuǎn)到這個(gè)頁(yè)面:
然后點(diǎn)擊頂端的“數(shù)據(jù)庫(kù)信息”
點(diǎn)擊右邊的“管理”
然后就到了登錄界面,登錄以后,在命令行頁(yè)面就可以操作數(shù)據(jù)庫(kù)了,建表、插入、刪除等等操作,寫完了以后 Ctrl+Enter 就執(zhí)行了
然后就可以執(zhí)行 bookmarks.sql 里面的響應(yīng)的 sql 語(yǔ)句,建好我們這個(gè)程序需要用到的幾個(gè)表
代碼如下:
然后我們需要插入一些測(cè)試數(shù)據(jù),直接用教材里面的吧:
insert into writers (username, password, full_name)
values ('bob', SHA1('password'), 'Robert Bobbins');
insert into writers (username, password, full_name)
values ('bill', SHA1('password'), 'William Billings');
insert into pages (code, description)
values ('news', 'The Top News Stories From Around the World');
insert into pages (code, description)
values ('sport', 'Sports Latest - All The Winners and Losers');
insert into pages (code, description)
values ('weather', 'Up To The Minute Weather Reports and Forecasts');
insert into writer_permissions (writer, page) values ('bob', 'news');
insert into writer_permissions (writer, page) values ('bob', 'sport');
insert into writer_permissions (writer, page) values ('bill', 'news');
insert into writer_permissions (writer, page) values ('bill', 'weather');
insert into stories
(id, writer, page, headline, created, modified, published,
story_text, picture)
values (1, 'bill', 'news', 'Man gives birth', 976573221, 976580154, 976570230,
'A man today gave birth in a hospital on Staten Island, NY. The baby boy weighed in at just over eight pounds and is doing well. The parents were naturally overjoyed at the birth of their first son, and have have said they hope to have a large family. <br /><br />Father Ted, 34, conceived using a new method known as paternatility whereby the fertilised embryo is transferred to the father\'s body at an early stage. It is believed that this method reduces many of the risks of childbirth.', 'images/1.jpg');
insert into stories
(id, writer, page, headline, created, modified, published,
story_text, picture)
values (2, 'bill', 'news', 'Fire!', 976562355, 976572203, 976570230,
'Breaking news: Reports are coming in of a fire in a barn somewhere in Arizona. Our sources say the barn is very likely to burn to the ground and will not be economically viable to repair.<br /><br />A bystander is reputed to have said "There was quite a lot of smoke"', 'images/2.jpg');
insert into stories
(id, writer, page, headline, created, modified, published,
story_text, picture)
values (3, 'bill', 'news', 'SFON Launch Party report', 976542355, 976542503, 976555650,
'Yesterday has already gone down in history as the day the best news site on the web first hit the Internet. Just to prove the point, there was a star-studded party last night at a secret location in Seattle.<br /><br />Joining our team for a boogie were several A-list celebs who wish to remain anonymous.', 'images/3.jpg');
insert into stories
(id, writer, page, headline, created, modified, published,
story_text, picture)
values (4, 'bob', 'sport', 'World Crossword Championship Preview', 976531355, 976532503, 976533320,
'It\'s now just three days to the start of the prestigious annual World Crossword Championship to be held this year for the first time live on the Internet. The new media format will allow many more competitors than ever before to take part from the comfort of their own home, or from one of 126 regional centers.<br /><br />Last year\' champion is not keen on the new format. She said "Crosswords should be done on paper, not online".', 'images/4.jpg');
insert into stories
(id, writer, page, headline, created, modified, published,
story_text, picture)
values (5, 'bob', 'sport', 'Basketball is bad for you', 976542355, 976542503, 976555650,
'Scientists believe that basketball can be bad for you. Research has suggested that both watching and playing the game can have detrimental effects on your health. The scientific evidence supporting this claim is currently being verified by our expert team and we will bring you updates as soon as we can.<br /><br />An NBA spokesperson said "That is complete rubbish".', 'images/5.jpg');
insert into stories
(id, writer, page, headline, created, modified, published,
story_text, picture)
values (6, 'bill', 'weather', 'Storms to come', 976542355, 976542503,
976555650, 'It never rains but it pours. When the rain comes in November there\'s gonna be a storm.<br /><br /> Meterologists predict rain, thunder, lightening and all the usual displeasures that you get during a period of inclement weather. No word on a hurricane yet, but don\'t be surprised if we get one!', 'images/6.jpg');
insert into stories
(id, writer, page, headline, created, modified, published,
story_text, picture)
values (7, 'bill', 'weather', 'Sun is shining, weather is sweet', 976451129, 976458734, 976458754,
'The forecast for this weekend is good, with long spells of sunshine predicted in most areas. The high temperature will be 96F and no rain is expected until November', '');
首選 Komodo edit,該有的功能都有了,左邊可以看到目錄,然后語(yǔ)法支持的也比較好,寫起代碼來(lái)很舒服
還可以用 Sublime Text2 這個(gè)也還不錯(cuò),我之前用了好久。
代碼下載:http://yun.baidu.com/share/link?shareid=3242370915&uk=2953066218然后我做好的這個(gè)示例網(wǎng)站的地址是 www.jiyuanhuida.cn/28 大家可以先去體驗(yàn)一下。
本文由 kaka 創(chuàng)作,采用 知識(shí)共享署名-相同方式 3.0 (CC協(xié)議) 中國(guó)大陸許可協(xié)議 進(jìn)行許可。轉(zhuǎn)載、引用前需聯(lián)系作者,并署名作者且注明文章出處。
更多建議: