Table of Contents
插入操作
Home php教程 php手册 PHP+MySql例子 - roucheng

PHP+MySql例子 - roucheng

May 20, 2016 am 10:14 AM

对于熟悉做网站的人来说,要想网站做成动态的,肯定要有数据库的支持,利用特定的脚本连接到数据库,从数据库中提取资料、向数据库中添加资料、删除资料等。这里我通过一个实例来说明如何用php连接到数据库的。 

  我准备建立一个简单的通讯录,数据库的名字叫txl,数据库只有一个表叫personal_info,表中有5个字段

pi_id pi_name pi_tel pi_qq pi_email

  首先我们要创建数据库:

  create database txl;

  然后我们建立表

  CREATE TABLE `personal_info` (

`pi_id` bigint(20) NOT NULL auto_increment,

`pi_name` varchar(50) NOT NULL,

`pi_tel` varchar(15) default NULL,

`pi_qq` varchar(15) default NULL,

`pi_email` varchar(50) default NULL,

PRIMARY KEY (`pi_id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

上面的sql语句很简单,通过字面都能猜出什么意思。

下面是连接到数据库并且显示表personal_info的所有字段信息:

// connsql.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

<span style="color: #000000;">php

 

    </span><span style="color: #800080;">$mysql_server_name</span>="localhost"; <span style="color: #008000;">//</span><span style="color: #008000;">数据库服务器名称</span>

 

    <span style="color: #800080;">$mysql_username</span>="root"; <span style="color: #008000;">//</span><span style="color: #008000;"> 连接数据库用户名</span>

 

    <span style="color: #800080;">$mysql_password</span>="root"; <span style="color: #008000;">//</span><span style="color: #008000;"> 连接数据库密码</span>

 

    <span style="color: #800080;">$mysql_database</span>="lxr"; <span style="color: #008000;">//</span><span style="color: #008000;"> 数据库的名字

 

  

 

    // 连接到数据库</span>

 

    <span style="color: #800080;">$conn</span>=<span style="color: #008080;">mysql_connect</span>(<span style="color: #800080;">$mysql_server_name</span>, <span style="color: #800080;">$mysql_username</span>,

 

                        <span style="color: #800080;">$mysql_password</span><span style="color: #000000;">);

 

  

 

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 从表中提取信息的sql语句</span>

 

    <span style="color: #800080;">$strsql</span>="select * from personal_info"<span style="color: #000000;">;

 

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 执行sql查询</span>

 

    <span style="color: #800080;">$result</span>=<span style="color: #008080;">mysql_db_query</span>(<span style="color: #800080;">$mysql_database</span>, <span style="color: #800080;">$strsql</span>, <span style="color: #800080;">$conn</span><span style="color: #000000;">);

 

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取查询结果</span>

 

    <span style="color: #800080;">$row</span>=<span style="color: #008080;">mysql_fetch_row</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">);

 

  

 

    </span><span style="color: #0000ff;">echo</span> '<font face="verdana">'<span style="color: #000000;">;

 

    </span><span style="color: #0000ff;">echo</span> '<table border="1" cellpadding="1" cellspacing="2">'<span style="color: #000000;">;

 

  

 

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 显示字段名称</span>

 

    <span style="color: #0000ff;">echo</span> "/n<tr>/n"<span style="color: #000000;">;

 

    </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span>=0; <span style="color: #800080;">$i</span>mysql_num_fields(<span style="color: #800080;">$result</span>); <span style="color: #800080;">$i</span>++<span style="color: #000000;">)

 

    {

 

      </span><span style="color: #0000ff;">echo</span> '<td bgcolor="#000F00"><b>'.

 

      <span style="color: #008080;">mysql_field_name</span>(<span style="color: #800080;">$result</span>, <span style="color: #800080;">$i</span><span style="color: #000000;">);

 

      </span><span style="color: #0000ff;">echo</span> "</b></td>/n"<span style="color: #000000;">;

 

    }

 

    </span><span style="color: #0000ff;">echo</span> "</tr>/n"<span style="color: #000000;">;

 

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 定位到第一条记录</span>

 

    <span style="color: #008080;">mysql_data_seek</span>(<span style="color: #800080;">$result</span>, 0<span style="color: #000000;">);

 

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 循环取出记录 何问起</span>

 

    <span style="color: #0000ff;">while</span> (<span style="color: #800080;">$row</span>=<span style="color: #008080;">mysql_fetch_row</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">))

 

    {

 

      </span><span style="color: #0000ff;">echo</span> "<tr>/n"<span style="color: #000000;">;

 

      </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span>=0; <span style="color: #800080;">$i</span>mysql_num_fields(<span style="color: #800080;">$result</span>); <span style="color: #800080;">$i</span>++<span style="color: #000000;"> )

 

      {

 

        </span><span style="color: #0000ff;">echo</span> '<td bgcolor="#00FF00">'<span style="color: #000000;">;

 

        </span><span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$row</span>[<span style="color: #800080;">$i</span>]"<span style="color: #000000;">;

 

        </span><span style="color: #0000ff;">echo</span> '</td>'<span style="color: #000000;">;

 

      }

 

      </span><span style="color: #0000ff;">echo</span> "</tr>/n"<span style="color: #000000;">;

 

    }

 

  

 

    </span><span style="color: #0000ff;">echo</span> "</table>/n"<span style="color: #000000;">;

 

    </span><span style="color: #0000ff;">echo</span> "</font>"<span style="color: #000000;">;

 

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 释放资源 hovertree.com</span>

 

    <span style="color: #008080;">mysql_free_result</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">);

 

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 关闭连接</span>

 

    <span style="color: #008080;">mysql_close</span><span style="color: #000000;">(); 

 

</span>?>

Copy after login

 

下面是运行结果:

pi_id pi_name pi_tel pi_qq pi_email

1 Zhangsan 13911111111 642864125 zhangsan@126.com

2 Lisi 13122222222 63958741 lisi@163.com

3 Wangwu 13833333333 912345678 wangwu@sohu.com

所谓“万变不离其宗”,再复杂的操作也都是基于上面的来的,都少不了上面的基本的步骤,当需要时查一下相关的手册便可以解决。

 

附参考代码:

1

2

3

4

5

<h1 id="插入操作">插入操作</h1> 

<span style="color: #000000;">php 

    </span><span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_POST</span>['submit'<span style="color: #000000;">])){ 

        </span><span style="color: #008000;">//</span><span style="color: #008000;">如果没有表单提交,显示一个表单  </span>

?> 

Copy after login
国家: 动物名称(英文): 动物名称(中文):
php } else { //如果提交了表单 hovertree.com //数据库连接参数 $host = "localhost"; $user = "root"; $pass = "zq19890319"; $db = "phpdev"; //取得表单中的值,检查表单中的值是否符合标准,并做适当转义,防止SQL注入 $country = empty($_POST['country'])? die("请输入国家名称"): mysql_escape_string($_POST['country']); $animal = empty($_POST['animal'])? die("请输入英文名"): mysql_escape_string($_POST['animal']); $cname = empty($_POST['cname'])? die("请输入中文名"): mysql_escape_string($_POST['cname']); //打开数据库连接 何问起 $connection = mysql_connect($host, $user, $pass) or die("Unable to connect!"); //选择数据库 mysql_select_db($db) or die("Unable to select database!"); //构造一个SQL查询 $query = "INSERT INTO symbols(country, animal, cname) VALUE('$country', '$animal', '$cname')"; //执行该查询 $result = mysql_query($query) or die("Error in query: $query. ".mysql_error()); //插入操作成功后,显示插入记录的记录号 echo "记录已经插入, mysql_insert_id() = ".mysql_insert_id(); //关闭当前数据库连接 mysql_close($connection); } ?>

推荐:http://www.cnblogs.com/roucheng/p/3528396.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)