php链接数据库

 

1:找到 ySQL服务器

  $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>";
}
?>
上一篇:Python核心编程--学习笔记--2--Python起步(上)


下一篇:MySql 1045错误