php连接到数据库

php连接到数据库,读取数据,添加修改数据等。

注意的有这几个函数:

1.连接到数据库;

//@这样做的可以屏蔽错误
$conn = @mysql_connect("localhost","root","") or die("数据库连接失败,请检查你的网络,稍后再试试");

 

2.选择数据库;

//选择数据库
mysql_select_db("test");

3.这个是必须写上的,必须每个编码集统一;

mysql_query("set names 'utf8'");

4.关闭数据库;

mysql_close($conn);

5.如何得到页面的数据,然后添加到数据库中;

$_POST[] 里的是input输入框中name属性的值。

 1 <?php
 2 if(@$_POST['submit']){
 3     $user = $_POST['user'];
 4     $sex = $_POST['sex'];
 5     $age = $_POST['age'];
 6     $phone = $_POST['phone'];
 7     $sdd = $_POST['sdd'];
 8     $conn = @mysql_connect("localhost","root","") or die("数据库连接失败,请检查你的网络,稍后再试试");
 9     mysql_select_db("sbb");
10     mysql_query("set names 'utf8'");
11     $sql = "INSERT INTO `sbb`.`t_student` (`t_id`, `t_name`, `t_sex`, `t_age`, `t_phone`, `t_sdd`) VALUES (NULL, '$user', '$sex', '$age', '$phone', '$sdd');";
12     mysql_query($sql);
13     mysql_close($conn);
14 }
15 ?>
 1  1  <body>
 2  2         <form action="text1.php" method="post">
 3  3             姓名: <input type="text" name="user"/> <br/>
 4  4             性别: <input type="text" name="sex"/> <br/>
 5  5             年龄: <input type="text" name="age"/><br/>
 6  6             电话: <input type="text" name="phone"/><br/>
 7  7             地址: <input type="text" name="sdd"/><br/>
 8  8             <input type="submit" name="submit" value="添加信息"/> <br/>
 9  9             <a href="text2.php">查看信息</a>
10 10         </form>
11 11     </body>

 



posted @ 2014-04-24 23:52  Sunshine丶  阅读(253)  评论(0编辑  收藏  举报