PHP + MySQL 搭建网站-准备工作
准备工作
本教程讲如何用 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 的格式。
所以不用觉得很困难,当然我在学习的过程中还是经历的很多痛苦的,因为书本资料比较久远,而且阿里的云主机也不知道怎么连数据库,所以很有必要记录下来。
按照惯例,我们来看下需要完成哪些准备工作吧。
首先你需要有一个自己的域名和主机。
这个我是通过阿里云注册的(www.net.cn)里面有域名申请,我申请了一个.cn
的,一年 29 块钱。域名申请很简单,掏钱就可以了。主要是之后还要申请主机,这样可以把你的域名解析成你的主机 ip 地址,然后用户访问你的域名以后就能跳转到你的主机上去,找到相应的网页文件了。
整个主机申请的过程可以参照网站上面的介绍,里面有特别详细的过程,包括填写资料,上传省份证、到指定地点拍照、邮寄资料,然后就等待审核吧。我之前的所有步骤,就是拍照、邮寄资料这些的都是两天就搞定了,但是最后资料审核特别慢,基本上两周才审核通过。你也可以申请国外的主机,好像不用审核,但是需要收费。国内阿里会免费提供你一个主机,但是就是需要审核。
这个是我在阿里的主机界面,在下面的新手必读里面,写的比较清楚,按照这个步骤来,你的主机申请是没啥问题的。
数据库操作
这个步骤我研究了好久才弄明白在哪里操作。本来按照上图里面的 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', '');
HTML编辑器
首选 Komodo edit,该有的功能都有了,左边可以看到目录,然后语法支持的也比较好,写起代码来很舒服
还可以用 Sublime Text2 这个也还不错,我之前用了好久。
总结
代码下载:http://yun.baidu.com/share/link?shareid=3242370915&uk=2953066218然后我做好的这个示例网站的地址是 www.jiyuanhuida.cn/28 大家可以先去体验一下。
本文由 kaka 创作,采用 知识共享署名-相同方式 3.0 (CC协议) 中国大陆许可协议 进行许可。转载、引用前需联系作者,并署名作者且注明文章出处。
更多建议: