一步一步教你用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  大家可以先去体验一下。

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

时间: 2024-08-14 20:28:47

一步一步教你用PHP+MySql搭建网站 No.0 准备工作的相关文章

一步一步教你用PHP+MySql搭建网站 No.3 管理页面

先来看一下本篇blog将要介绍的内容. 我们的主页面已经搭建完成了,然后左边的navigation里面的大部分内容也都能点击了,只剩下"search" 和"I'm a writer"两个链接了. 本篇教程过后,我们将能完成下面的功能: 这是我们的主界面 点击"I'm a writer" : 点击任意一个链接: Writers登录之后的页面: 好了,我们一步一步来吧: 我们可以看到在主界面的表单项里面,点击I'm a writer之后的链接是这样的

一步一步教你用PHP+MySql搭建网站 No.5 图片上传、故事删除

上篇文章中讲到,story.php中的表单提交之后的页面是story_submit.php,我们就看一下story_submit.php是如何完成文章的发表的 老样子,先上代码: <?php # add / modify story record include_once('include_fns.php'); $handle = db_connect(); $headline = $_REQUEST['headline']; $page = $_REQUEST['page']; $time =

一步一步教你用PHP+MySql搭建网站 No.6 文章的publish

本节我们来看和publish相关的3个页面:  publish.php, unpublish_story.php和publish_story.php 我们回到admin的主页面,index.php 我们点击下面的链接,跳转到的是 publish.php 整体过一遍代码,这里面应该没有多少新的内容了,我们重点看一下结构: 4-6行 和editer一样,如果当前用户还未已经登录,就显示登录的界面 9-13行 显示当前登录作者的名字,以及3个链接,logout,主菜单和返回主页面 15-18行 这个s

一步一步教你用PHP+MySql搭建网站 No.7 关键字和搜索

本篇是这系列教程的最后一篇了. 我们看到还有几个页面没有讲到,分别是 keywords相关页面 和search相关页面 将这两部分放在一起是因为search是搜索的关键字找到文章的.因为这本教材是比较老的教材了,所以里面的架构设计可能也比较老. 截止到目前,基本上没有新的知识点要讲解了,看一下如何来构建网页和后台的逻辑吧: keywords.php 第10行 get_story_record 和get_writer_record功能类似,是在db_fns.php中的一个函数: keyword_a

一步一步教你用PHP+MySql搭建网站 No.2 图片显示和Page页面

接着上一章节的看,我们现在的程序有了第一个主页面,但是我们会发现几个问题,一个是主页上的图片没法显示,二是点击read more ... 之后的链接没有内容 这两个页面的入口分别是下图里面的两个框出来的内容. 我们先来看图片显示的页面: Resize_image.php 我们注意到其实这个链接的完整URL是: resize_image.php?image=...&max_width=...&max_height=...; 也就是说这个链接有3个参数: image是图片的名称,max_wid

一步一步教你用PHP+MySql搭建网站 No.1 主页&amp;数据库连接

这一章节我们来看用户输入网页后的主界面. 一般来说,默认主页都是index点xxx,比如 index.php, index.html , index.jsp等等.我们来看一下我们的index.php吧 index.php 提醒: 在<?php?>的两端,不要出现任何空格或者其他字符,也就是不要在<?php 的前面加上空格这样的字符,然后?>后面也不要出现其余的字符,不然的话可能会出现这样的提示: 我之前出现过这样的情况,google了之后是说headers 必须放在最开始的时候执行

一步一步教你用PHP+MySql搭建网站 No.4 文章编辑、图片上传

本篇blog我们将来重点看文章编辑页面story.php,因为这个页面说实话代码量是挺多的,还涉及到了图片的上传. 从页面上来直观的体验: add new  和 edit都是打开的story.php页面,所以我们应该能提前想到,这个页面会先检测下是哪种请求. 首先我们来搞定比较简单的logout.php页面 这个页面其实很简单了,主要是几个函数 unset函数其实就是将一些特定的变量置为空: session_destroy函数是销毁当前的session,当然,当前session中的数据也随着一并

web系列教程之php 与mysql 动态网站 。检索 与更新。

接着上次WEb 系列开发之php 与mysql动态网站入门. 个人觉得,学习技术就像一棵大树,主干很重要,枝叶其次.对于学习技术,我们应该分清主次关系.怎么学?为什么要学?有一个较好的分寸. 有时候觉得 国内有些教育,从一开始就从枝叶 细节说的很清楚.说了一大堆,就是很散的感觉,虽然有很多知识.但是学的人呢,并不清楚这个的实际应用, 这样会导致学习兴趣的缺失.个人觉得 就比如 php 和mysql吧,虽然我也不是高手,但就学习上面还是能评论几句的,比如我从表单开始 讲表单的注册,也就是 网站会员

教你一步一步部署.net免费空间OpenShift系列之一------帐号注册和验证

前几天有博友发布了一篇文章<一键部署mono 免费空间支持ASP.NET MVC 再也不担心伙食费换空间了>,支持MVC3和域名绑定,觉得不错,于是自己实践了一下,发现自己实际遇到的问题真不少,而且网上的关于此空间的帖子要么千篇一律,要么语焉不详,现总结为图文教程系列 帐号注册和验证 打开https://www.openshift.com/products/pricing,出现三种选择,前2种是免费的,建议选择第二个. 点击Create one跳转到创建用户界面 看到如下信息,填写邮箱和密码,