一步一步教你用PHP+MySql搭建网站 No.0 准备工作

新开一个系列教程吧,这次是讲如何用PHP+MySQL搭建网站。

之前一直有想过搭建自己的个人网站,然后上周通过阿里云申请的域名和空间都通过审核了,于是就开始研究如何用PHP+MYSQL搭建网站,研究了差不多两周,总算搞定了一个小型的blog类的网站。当然,整个过程是通过学习《PHP and MySQL web development》下来的,这篇教程中的例子是在原书中的Chapter 28: Building a Content Manager System章节的基础上,修改完成的。所有的源代码可以在这里找到,本来想买中文版的书来看,但是看评论说翻译的不行,然后就直接看的原版第3版的英文PDF,有想要的同学可以自行google或者给我留言。

我是从0开始学习PHP+MySQL的,基本上网站搭建下来花了不到两周;当然,这里需要你有一定的HTML和SQL的基础,很基本的就行了。PHP其实就是个脚本语言,在原来的HTML中,把需要动态计算的部分用PHP完成,其实最后输出出来的还是HTML的格式。

所以不用觉得很困难,当然我在学习的过程中还是经历的很多痛苦的,因为书本资料比较久远,而且阿里的云主机也不知道怎么连数据库,所以很有必要记录下来。

按照惯例,我们来看下需要完成哪些准备工作吧。

1. 首先你需要有一个自己的域名和主机。

这个我是通过阿里云注册的(www.net.cn)里面有域名申请,我申请了一个.cn的,一年29块钱。域名申请很简单,掏钱就可以了。主要是之后还要申请主机,这样可以把你的域名解析成你的主机ip地址,然后用户访问你的域名以后就能跳转到你的主机上去,找到相应的网页文件了。

整个主机申请的过程可以参照网站上面的介绍,里面有特别详细的过程,包括填写资料,上传省份证、到指定地点拍照、邮寄资料,然后就等待审核吧。我之前的所有步骤,就是拍照、邮寄资料这些的都是两天就搞定了,但是最后资料审核特别慢,基本上两周才审核通过。你也可以申请国外的主机,好像不用审核,但是需要收费。国内阿里会免费提供你一个主机,但是就是需要审核。

技术分享

这个是我在阿里的主机界面,在下面的新手必读里面,写的比较清楚,按照这个步骤来,你的主机申请是没啥问题的。


2. 数据库操作

这个步骤我研究了好久才弄明白在哪里操作。本来按照上图里面的3步骤来,本地下了个Mysql然后无论如何也连接不到远程去。后来无意间发现可以在网页上管理数据库。

就是上图里面的,在你的主机目录后面,点击“管理”,然后就跳转到这个页面:

技术分享

然后点击顶端的“数据库信息

技术分享

点击右边的“管理

技术分享

然后就到了登录界面,登录以后,在命令行页面就可以操作数据库了,建表、插入、删除等等操作,写完了以后 ctrl+enter就执行了 

技术分享

然后就可以执行bookmarks.sql里面的响应的sql语句,建好我们这个程序需要用到的几个表

代码如下:

技术分享


然后我们需要插入一些测试数据,直接用教材里面的吧:

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', '');

3. 好像基本也没啥了,然后推荐几个比较好用的HTML编辑器吧。

我用的两个感觉还不错 。

首选  Komodo edit,该有的功能都有了,左边可以看到目录,然后语法支持的也比较好,写起代码来很舒服

技术分享

还可以用Sublime Text2 这个也还不错,我之前用了好久。

4. 最后提醒下,和上一系列教程一样,这次我依旧用截图的方式来讲解,如果想要源代码的同学可以直接到这里下载,但是我还是强烈建议大家亲自把代码敲上去,这样感受下php代码的写法,比你直接复制粘贴效果来的好!

然后我做好的这个示例网站的地址是  www.jiyuanhuida.cn/28  大家可以先去体验一下。


好了,马上正式开始我们的教程!

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。