码迷,mamicode.com
首页 > 数据库 > 详细

php连接到数据库操作

时间:2014-04-29 15:35:54      阅读:582      评论:0      收藏:0      [点我收藏+]

标签:com   http   class   blog   style   div   code   ext   string   log   color   

1
2
3
4
5
6
7
8
9
10
11
<?php
$result = mysql_query($sql);
            while($row = mysql_fetch_array($result)) {
?>
 
要写的内容代码,比如说Html
 
 
<?php
}
?>

 

可以在html中添加数据提交到数据库中,前提要和数据库中名字信息一致

表单提交:

1
<form action="showDaYin.php" method="post"><br>        .......<br>      <input type="submit" name="submit" value="添加"><br></form>

 这是一部分提交表单中的数据的代码,其中包含有链接到数据库的

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
<html>
<head>
    <title>添加信息</title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
    <?php
    if(@$_POST[‘submit‘]){
        $id = $_POST[‘id‘];
        $name = $_POST[‘name‘];
        $address = $_POST[‘address‘];
        $age = $_POST[‘age‘];
        $sex = $_POST[‘sex‘];
        $hobby = $_POST[‘hobby‘];
        $fav = "";
        foreach($hobby as $v){
            $fav.=$v.",";
        }
        if($sex){
            $sex = "男";
        }else{
            $sex = "女";
        }
        echo "学号:".$id."<br/>";
        echo "姓名:".$name."<br/>";
        echo "地址:".$address."<br/>";
        echo "年龄:".$age."<br/>";
        echo "性别:".$sex."<br/>";
        echo "爱好:".$fav."<br/>";
         
        $conn = mysql_connect("localhost","root","") or die("数据库连接失败!!");  <span style="color: rgb(204, 153, 255);">//找到mysql服务器</span>
        mysql_select_db("test");  <span style="color: rgb(204, 153, 255);"> //选择链接到的数据库</span>
        mysql_query("set names ‘utf8‘"); <span style="color: rgb(204, 153, 255);"> //设置编码</span>
        $sql = "INSERT INTO `test`.`students`(`id`, `names`, `address`, `age`, `sex`, `hobby`)VALUES (null,‘$name‘,‘$address‘,$age,‘$sex‘,‘$fav‘)";
        mysql_query($sql) or die(mysql_error());  <span style="color: rgb(204, 153, 255);">//执行数据库操作</span>
        mysql_close($conn);
    }
    ?>
</body>
</html>

 

只能读出返回运行一次的值:

1
2
$result = mysql_query($sql);
$row = mysql_fetch_array($result)

 要显示库中所有数据需要加上循环

1
2
3
4
5
6
7
8
9
<?php
       $result = mysql_query($sql);
           while($row = mysql_fetch_array($result)){
   ?>
   //内容代码
 
   <?php
           }
   ?>

 ieset 得到的是布尔类型的值(1  or  0)

 

php连接到数据库操作,布布扣,bubuko.com

php连接到数据库操作

标签:com   http   class   blog   style   div   code   ext   string   log   color   

原文地址:http://www.cnblogs.com/5huihui/p/3698295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!