php连接mysql的增删改查
<?php
header("Content-type:text/html;charset=utf-8");
//1.创建连接对象(登录)
$conn = mysql_connect("localhost","root","root");
if($conn){
//2.连接库
echo "连接成功"."<br>";
mysql_select_db("2109");
//3.sql语句的操作 mysql_query(sql,连接对象);
//增删该DML
//a.增
//mysql_query("insert into student values (7,'老王',18,'2021-9-13')",$conn);
//mysql_query("delete from student where stu_id = 7",$conn);
//mysql_query("update student set stu_age = 18 where stu_name = '常璐璐'",$conn);
//查 DQL
//返回值为结果集
//$result = mysql_query("select * from student",$conn);
//$result = mysql_query("select * from student where stu_id = 22",$conn);
//mysql_num_rows(结果集):返回结果集拥有的记录数
//$rows = mysql_num_rows($result);
//echo $rows;
//mysql_fetch_assoc(结果集):返回当前游标所指向的记录,以对象的方式返回.
//每当执行一次该函数,游标自动下移
$result = mysql_query("select * from student",$conn);
while($obj = mysql_fetch_assoc($result)){
echo $obj["stu_id"]." ".$obj["stu_name"]." ".$obj["stu_age"]." ".$obj["stu_date"]."<br>";
}
//4.关闭资源
mysql_close($conn);
php接受前端数据
<?php
header("Content-type:text/html;charset=utf-8");
//接收前端数据
$id = $_POST["userId"];
$name = $_POST["userName"];
$conn = mysql_connect("localhost","root","root");
mysql_select_db('2109');
$result = mysql_query("select * from student where stu_id = $id and stu_name = '$name'",$conn);
if(mysql_num_rows($result) == 1){
echo "不能注册";
}else{
mysql_query("insert into student values ($id,'$name',18,'2021-9-13')",$conn);
}
mysql_close($conn);
?>
cookie
//设置cookie
// document.cookie = "key=value";
//cookie的使用必须有服务器
document.cookie = "name=laowang";
document.cookie = "age=18";
document.cookie = "name=laolv";
//获取cookie:获取的数据为键值对字符串,
//两个字符串用分号+空格分开
//age=18; name=laolv
console.log(document.cookie);
//会话级别cookie
//document.cookie = "key=value";
//长生命周期
//document.cookie = "key=value;expires="+标准日期对象;
// let date = new Date();
// date.setDate(date.getDate()+7);
// document.cookie = "name=老王;expires="+date;
//console.log(document.cookie);
//console.log(document.cookie);
//关闭也面时触发
// window.onbeforeunload = function(){
// let date = new Date();
// date.setDate(date.getDate()+7);
// document.cookie = "age=18;expires="+date;
// }
、