php链接数据库

 

 

1:找到 ySQL服务器

1  $conn = mysql_connect("localhost","","") or die("链接数据库失败");

 mysql_connect("本机","用户名","密码")


2:选择要连接的数据库

mysql_select_db("test");

3:设置编码

 mysql_query("set names 'utf8'");


4:执行数据库操作

    $sql = "INSERT INTO `stu_logon`(`id`, `stuname`, `add`, `age`, `sex`, `fav`) VALUES (NULL,'$name','$add',$age,$sex,'$fav')";
    mysql_query($sql) or die(mysql_error());

 or die(mysql_error():提示错误

 

5:关闭数据库

 mysql_close($conn);

 

<?php
if(isset($_GET['stuid'])){//isset的值返回的值是true或false
    $id = $_GET['stuid'];//    form的method有两种:
                       //       post和get(通过url提交),我们常用的是post



    $conn = mysql_connect("localhost","root","root") or die("链接数据库失败");
    mysql_select_db("test");
    mysql_query("set names 'utf8'");

    $sql = "select * from student where id = $id";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    echo $row['address'];

}else{
    echo "<script>alert('you dont request this page by url!,please into by A tag');</script>";
}
?>

 

 

posted @ 2014-04-28 21:52  moemiss  阅读(264)  评论(0编辑  收藏  举报