PHP + MySQL 搭建網(wǎng)站-準備工作

2018-09-28 20:12 更新

準備工作

本教程講如何用 PHP+MySQL 搭建網(wǎng)站。

之前一直有想過搭建自己的個人網(wǎng)站,然后上周通過阿里云申請的域名和空間都通過審核了,于是就開始研究如何用 PHP+MYSQL 搭建網(wǎng)站,研究了差不多兩周,總算搞定了一個小型的 blog 類的網(wǎng)站。當然,整個過程是通過學習《PHP and MySQL web development》下來的,這篇教程中的例子是在原書中的 Chapter 28: Building a Content Manager System 章節(jié)的基礎上,修改完成的。所有的源代碼可以在這里找到,本來想買中文版的書來看,但是看評論說翻譯的不行,然后就直接看的原版第 3 版的英文 PDF,有想要的同學可以自行 Google 或者給我留言。

我是從 0 開始學習PHP+MySQL的,基本上網(wǎng)站搭建下來花了不到兩周;當然,這里需要你有一定的 HTML 和 SQL 的基礎,很基本的就行了。PHP 其實就是個腳本語言,在原來的 HTML 中,把需要動態(tài)計算的部分用 PHP 完成,其實最后輸出出來的還是 HTML 的格式。

所以不用覺得很困難,當然我在學習的過程中還是經(jīng)歷的很多痛苦的,因為書本資料比較久遠,而且阿里的云主機也不知道怎么連數(shù)據(jù)庫,所以很有必要記錄下來。

按照慣例,我們來看下需要完成哪些準備工作吧。

首先你需要有一個自己的域名和主機。

這個我是通過阿里云注冊的(www.net.cn)里面有域名申請,我申請了一個.cn的,一年 29 塊錢。域名申請很簡單,掏錢就可以了。主要是之后還要申請主機,這樣可以把你的域名解析成你的主機 ip 地址,然后用戶訪問你的域名以后就能跳轉到你的主機上去,找到相應的網(wǎng)頁文件了。

整個主機申請的過程可以參照網(wǎng)站上面的介紹,里面有特別詳細的過程,包括填寫資料,上傳省份證、到指定地點拍照、郵寄資料,然后就等待審核吧。我之前的所有步驟,就是拍照、郵寄資料這些的都是兩天就搞定了,但是最后資料審核特別慢,基本上兩周才審核通過。你也可以申請國外的主機,好像不用審核,但是需要收費。國內阿里會免費提供你一個主機,但是就是需要審核。

這個是我在阿里的主機界面,在下面的新手必讀里面,寫的比較清楚,按照這個步驟來,你的主機申請是沒啥問題的。

數(shù)據(jù)庫操作

這個步驟我研究了好久才弄明白在哪里操作。本來按照上圖里面的 3 步驟來,本地下了個 Mysql 然后無論如何也連接不到遠程去。后來無意間發(fā)現(xiàn)可以在網(wǎng)頁上管理數(shù)據(jù)庫。

就是上圖里面的,在你的主機目錄后面,點擊“管理”,然后就跳轉到這個頁面:

然后點擊頂端的“數(shù)據(jù)庫信息”

點擊右邊的“管理”

然后就到了登錄界面,登錄以后,在命令行頁面就可以操作數(shù)據(jù)庫了,建表、插入、刪除等等操作,寫完了以后 Ctrl+Enter 就執(zhí)行了

然后就可以執(zhí)行 bookmarks.sql 里面的響應的 sql 語句,建好我們這個程序需要用到的幾個表

代碼如下:

然后我們需要插入一些測試數(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', ''); 

HTML編輯器

首選 Komodo edit,該有的功能都有了,左邊可以看到目錄,然后語法支持的也比較好,寫起代碼來很舒服

還可以用 Sublime Text2 這個也還不錯,我之前用了好久。

總結

代碼下載:http://yun.baidu.com/share/link?shareid=3242370915&uk=2953066218然后我做好的這個示例網(wǎng)站的地址是 www.jiyuanhuida.cn/28 大家可以先去體驗一下。

本文由 kaka 創(chuàng)作,采用 知識共享署名-相同方式 3.0 (CC協(xié)議) 中國大陸許可協(xié)議 進行許可。轉載、引用前需聯(lián)系作者,并署名作者且注明文章出處。

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號