有关数据库的操作(命令)
一.插入数据
- 向表格的所有字段赋值
insert into users values(null,‘Mike’,md5(‘123abc’),1,‘1996-1-1’,‘a.jpg’,‘[email protected]’) - 向表格的部分字段赋值(其他字段必须满足条件:允许为空或者有默认值)
insert into users(email,uname,password,gender) values(‘[email protected]’,‘Rose’,md5(123),0) - 一个insert插入多条记录
insert into users(email,uname,password,gender) values(‘[email protected]’,‘Rose’,md5(123),0),(‘[email protected]’,‘Jack’,md5(123),0),(‘[email protected]’,‘Andy’,md5(123),0)
二.where子句
(1)算术运算符
(2)比较运算符 != <>
(3)逻辑运算符 and or not
(4)范围 between and
(5)集合 in not in
(6)模糊查询 like 通配符% _
(7)判断空 is null is not null
三.删除数据
- delete from users
四.修改数据
- 删除所有用户的密码
update users set password=null - 把uid为1 的用户的用户名改为java
update users set username = ‘java’ where uid = 1
五.查询数据
- 查询所有男生 select * from users where gender = 1
- 查询所有邮箱为空的用户select * from users where email is null
- 查询1999年出生的用户select * from users where year(birthdate)=1999
- 查询九零后的用户信息select * from users where year(birthdate) between 1990 and 1999
- 查询uid为1,3,5 的用户select * from users where uid in(1,3,5)
- 查询邮箱为qq邮箱的用户select * from users where email like ‘%@qq.com’
- 查询用户名为两个字的用户select * from users where uname like ‘__’
- 第二个字是晓的用户select * from users where uname like ‘_晓%’
- 查询uid大于平均uid的用户(where条件里不能使用聚合函数(count avg sum max min);)
select * from users where uid >(select avg(uid) from users) - 将uid大于平均uid的用户的性别改为1
update users set gender=1 where uid >(select ua from(select avg(uid) as ua from users) u)
delete from users where uid >(select avg(uid) from users)
- 查询所有用户按照由老到小排列select * from users order by birthdate asc
- 查询所有用户按照由小到老排列select * from users order by birthdate desc
六.limit
- 限制查询个数 limit
- limit n 表示查询前n条记录
- select * from users order by birthdate asc limit 3
- limit m,n m代表起始位置(从0开始),n代表查询个数
- 从第2行开始查询五个用户select * from users limit 1,5
- limit用来实现分页显示
七.两表连接
两表连接:查询所有的视频名称以及所属的视频类型名称
select videoname,typename from videos v ,videotype t
where v.tid=t.tid
select videoname,typename from videos v join videotype t on v.tid=t.tid
查询每种视频类型下有多少个视频(查询内容中包含普通字段和聚合字段,必须使用分组查询group by )
select count(vid) vc,t.tid,typename from videotype t,videos v where t.tid=v.tid group by t.tid,typename
select count(vid) vc,t.tid,typename from videotype t left join videos v on t.tid=v.tid group by t.tid,typename
一.新建一个mysql用户
方法一
-
grant all privileges on . to phper2@localhost identified by ‘123’ with grant option;
grant 权限 on 数据库.表 to 用户@主机 identified by 密码 with grant option;
运行完毕,查看mysql里的user表 -
grant select on neuvideo.* to phper3@localhost identified by ‘123’ with grant option;
运行完毕,查看mysql里的user表 db表
方法二
insert into user values(‘localhost’,‘phper4’,password(‘123’),‘Y’,…
想要生效 需重启服务器或者执行flush privileges;刷新权限库
此方法过于繁琐
删除用户
方法1:drop user phper@localhost;
方法2:delete from user where … 然后执行flush privileges;
修改密码
方法1:set password for root@localhost=(‘123456’);
方法2:update
- 练习一个以自己姓名前缀命名的用户,只对neuvideo数据库具备所有权限(注意权限问题!!!)
grant all privileges on neuvideo.* to wsw@localhost identified by ‘123’ with grant option;
( wsw是用户名,123是密码)
找mysql的bin根目录
2018-11.2
db.php
-
mysql_connect() 函数,语法:mysql_connect(server,user,pwd,newlink,clientflag)后两个参数可选。
@mysql_connect(“localhost:3306”,“wsw”,“123”) or die(“连不上服务器”); -
选择要操作的数据库 mysql_select_db(“neuvideo”) or die(“找不到这个数据库”);
-
在执行sql语句之前设置mysql连接的编码 mysql_query(“set names utf8”);
-
执行sql $sql=“select * from videotype”;
dp.php源码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>order</title>
</head>
<body>
<?php
// /**
// * Created by PhpStorm.
// * User: wangs
// * Date: 2018/10/31
// * Time: 14:17
// */
//访问数据库最简单——mysql面向过程
//连接数据库服务器——地址IP
@mysql_connect("localhost:3306","wsw","123") or die("连不上服务器");
//选择要操作的数据库
mysql_select_db("neuvideo") or die("找不到这个数据库");
//在执行sql语句之前设置mysql连接的编码
mysql_query("set names utf8");
//执行sql
$sql="select * from videotype";
//执行sql语句//Java中查找和修改删除不同
$res=mysql_query($sql);
var_dump($res);
//从结果中取出一行数据
$row=mysql_fetch_array($res);//用数组保存
var_dump($row);
// $row=mysql_fetch_array($res);//用数组保存
// var_dump($row);
// $row=mysql_fetch_array($res);//用数组保存
// var_dump($row);
// $row=mysql_fetch_array($res);//用数组保存
// var_dump($row);
// $row=mysql_fetch_array($res);//用数组保存
// var_dump($row);
// $row=mysql_fetch_array($res);//用数组保存
// var_dump($row);
echo "<ul >";
while ($row=mysql_fetch_array($res)){
echo "<li style='float: left;margin-left: 20px;list-style: none;'>";
//var_dump($row);
echo $row['typename'];
echo "</li>";
}
echo "</ul>";
?>
</body>
</html>
因为很多语句都被淘汰了所以写了dp1.php版本作为新的写法
dp1.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>order</title>
</head>
<body>
<?php
// /**
// * Created by PhpStorm.
// * User: wangs
// * Date: 2018/10/31
// * Time: 14:17
// */
//访问数据库最简单——mysql面向过程
//连接数据库服务器——地址IP
$con = mysqli_connect("localhost:3306","wsw","123","neuvideo") or die("连不上服务器");
//选择要操作的数据库
//mysqli_select_db("neuvideo") or die("找不到这个数据库");
//在执行sql语句之前设置mysql连接的编码
mysqli_query($con,"set names utf8");
//执行sql
$sql="select * from videotype";
//执行sql语句//Java中查找和修改删除不同
$res=mysqli_query($con,$sql);
var_dump($res);
echo "<ul >";
while ($row=mysqli_fetch_array($res)){
echo "<li style='float: left;margin-left: 20px;list-style: none;'>";
//var_dump($row);
echo $row['typename'];
echo "</li>";
}
echo "</ul>";
//以下两句不写,服务器也也会自动更新
mysqli_free_result($res);//释放内存
mysqli_close($con);//关闭连接
?>
</body>
</html>
向数据库neuvideo中的表videotype插入一个名称为“搞笑”的内容(常规写法)
<?php
$con = mysqli_connect("localhost:3306","wsw","123","neuvideo") or die("连不上服务器");
mysqli_query($con,"set names utf8");
//插入sql语句出现bolean true是真为插入
$sql="insert into videotype VALUES ('null','搞笑')";
$res=mysqli_query($con,$sql);
var_dump($res);
?>
向之前reg.php这个注册信息的表单,连接doUserRegcopy.php文件。实现:判断注册的用户是否注册过,如果注册过输出该用户存在。
在db表中插入用户名是wsw密码为123的数据,连接数据库
doUserRegcopy.php源码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>处理注册信息</title>
</head>
<body>
<?php
var_dump($_FILES);
//文件名
$arr=explode(".",$_FILES["pic"]["name"]);//把原文件名切割
$ex=$arr[count($arr)-1];//获得扩展名
$fname=date("YmdHis").rand(100,999).".".$ex;
move_uploaded_file($_FILES["pic"]["tmp_name"], "upload/$fname");///////将上传的文件放到upload里
//获取信息
$un=$_POST["username"];
$pw=$_POST["password"];
$gender=$_POST["gender"];
$hobby=implode(",",$_POST["hobby"]);
$email=$_POST["email"];
$birth=$_POST["birthdate"];
$con = mysqli_connect("localhost:3306","wsw","123","neuvideo") or die("连不上服务器");
mysqli_query($con,"set names utf8");
//判断用户是否存在,存在不用再注册
$sql="select * from users where uname='$un'";
$res=mysqli_query($con,$sql);
if (mysqli_num_rows($res)){
echo "<script>alert('该用户名已存在,请重新注册');
location.href='reg.php';//从这个文件读取?????????
</script>";
}
$sql="INSERT into users values(null,'$un',md5('$pw'),$gender,'$birth','$fname','$email')";//要和你users表的参数相对应
echo $sql;//测试,拷贝浏览器的sql语句,到数据库里执行
if (mysqli_query($con,$sql)){
echo "成功";
}else{
echo "失败";
}
?>
<table border="1" width="400px">
<tr>
<td>用户名</td>
<td><?php echo $_POST["username"]; ?></td>
</tr>
<tr>
<td>密码</td>
<td><?php echo $_POST["password"]; ?></td>
</tr>
<tr>
<td>性别</td>
<td><?php echo ($_POST["gender"]==0)?"男":"女"; ?></td>
</tr>
<tr>
<td>生日</td>
<td><?php echo $_POST["password"]; ?></td>
</tr>
<tr>
<td>爱好</td>
<td><?php echo implode(",",$_POST["hobby"]); ?></td>
</tr>
<tr>
<td>邮箱</td>
<td><?php echo $_POST["email"]; ?></td>
</tr>
<tr>
<td>头像</td>
<td><img src="upload/<?php echo $fname; ?>" width="200px"></td>
</tr>
</table>
</body>
</html>
reg.php源码
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>视频信息管理系统</title>
</head>
<body>
<form method=post action="doUserRegcopy.php" enctype="multipart/form-data">
<div class="col-md-10">
<input type="text" name="username" class="form-control" id="exampleInputUserName" placeholder="姓名" required>
</div>
<div class="col-md-10">
<input type="password" name="password" class="form-control" id="exampleInputPassword" placeholder="密码" required>
</div>
<div class="col-md-10">
<input type="radio" name="gender" value=0 checked> 男
<input type="radio" name="gender" value=1> 女
</div>
<div class="col-md-10">
<input type="date" name="birthdate" class="form-control" id="exampleInputBirth" placeholder="生日" required>
</div>
<div class="col-md-10">
<input type="checkbox" name="hobby[]" value="运动">运动
<input type="checkbox" name="hobby[]" value="阅读">阅读
<input type="checkbox" name="hobby[]" value="音乐">音乐
<input type="checkbox" name="hobby[]" value="外语">外语
<input type="checkbox" name="hobby[]" value="舞蹈">舞蹈
</div>
<!-- pic上传文件-->
<input type="hidden" name="MAX_FILE_SIZE" value="3145728">
<div class="col-md-10">
<input type="file" name="pic" id="exampleInputFile" required>
</div>
<div class="col-md-10">
<input type="email" name="email" class="form-control" id="exampleInputEmail" placeholder="常用邮箱" required>
</div>
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" value="注册">
<input type="reset" class="btn btn-default" value="重置">
</div>
</form>
</body>
</html>
现在我们看一下效果:
打开服务器然后找到reg.php输入信息
php文件会从reg.php到doUserRegcopy.php
如果出现错误我们将浏览器上的代码输入到Navicat进行查询操作可以知道是否有问题
在表中查看
如果我们输入的用户名表中有 会出现什么呢?
这个unload文件中负责装我们上传的文件内容