您的位置:首页 > 编程语言 > PHP开发

php连接到数据库

2014-04-24 23:52 148 查看
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属性的值。
<?php
if(@$_POST['submit']){
$user = $_POST['user'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$phone = $_POST['phone'];
$sdd = $_POST['sdd'];
$conn = @mysql_connect("localhost","root","") or die("数据库连接失败,请检查你的网络,稍后再试试");
mysql_select_db("sbb");
mysql_query("set names 'utf8'");
$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');";
mysql_query($sql);
mysql_close($conn);
}
?>
1  <body>
2         <form action="text1.php" method="post">
3             姓名: <input type="text" name="user"/> <br/>
4             性别: <input type="text" name="sex"/> <br/>
5             年龄: <input type="text" name="age"/><br/>
6             电话: <input type="text" name="phone"/><br/>
7             地址: <input type="text" name="sdd"/><br/>
8             <input type="submit" name="submit" value="添加信息"/> <br/>
9             <a href="text2.php">查看信息</a>
10         </form>
11     </body>

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: