PHP 数据库操作
连接数据库
连接数据库常用的函数
mysqli_connect(主机IP,用户名,密码,数据库名,端口号) // 端口号如果是3306可以省略
mysqli_connect_error():获取连接数据库的错误信息
mysqli_connect_erron():获取连接数据库的错误代码
mysqli_set_charset(连接对象,字符编码)
连接示例:连接失败的例子
<?php
header('Content-type:text/html;charset=utf-8');
# 连接数据库
$conn = @mysqli_connect('localhost','root','Admin123','junglezts','3306');
# 设置数据库字符编码,这里如果连接失败是无法设置的。
mysqli_set_charset($conn,'utf8');
# 如果连接失败
if(mysqli_connect_error()){
// 输入错误代号,错误信息。
echo "错误号:".mysqli_connect_errno(),"<br>";
echo "错误信息:".mysqli_connect_error();
}
?>
操作数据库
数据库操作语句,增加、删除、更改的结果返回都是True
和Fasle
-- 增加
insert into 表名 (字段1,字段2,字段...) values (字段值1,字段值2,字段值...);
-- 删除,整行伤处数据
delete from 表名 where 字段名 = 字段值 and ...;
-- 更改
update 表名 set 字段名 = '字段值' where 字段名 = 字段值 and ...
常用的PHP函数
mysqli_query():执行SQL语句
mysqli_query_id():获取插入纪录自动增长的ID
mysqli_affected_rows():受影响的记录数
mysqli_error():SQL语句执行的错误信息
mysqli_errno():SQL语句执行的错误码
执行insert SQL语句
:
<?php
# 操作数据库
$conn = mysqli_connect('localhost','root','Admin123','junglezt','3306');
# 设置字符编码
mysqli_set_charset($conn,"utf8");
# 执行SQL语句
$sql = "insert into user values (1,'zhangsan','123123')";
$mysql_resp = mysqli_query($conn,$sql);
# 查询返回结果
var_dump($mysql_resp);
?>
成功执行SQL语句
测试函数
<?php
# 操作数据库
$conn = mysqli_connect('localhost','root','Admin123','junglezt','3306');
# 设置字符编码
mysqli_set_charset($conn,"utf8");
# 执行SQL语句
$sql = "insert into user values ( ,'LQL','azt1314')";
$mysql_resp = mysqli_query($conn,$sql);
if ($mysql_resp){
echo "自动增长的编号是:".mysqli_insert_id($conn),"<br/>";
echo "受影响的行数:".mysqli_affected_rows($conn);
}else{
echo "SQL查询错误信息:".mysqli_error($conn),"<br/>";
echo "SQL查询错误码:".mysqli_errno($conn);
}
?>
操作返回数据函数
mysqli_fetch_row() -- 将对象中的一条数据匹配成索引数组(数字下标),指针下移一条
mysqli_fetch_assoc() -- 将对象中的一条数据匹配成关联数组(字段下标),指针下移一条
mysqli_fetch_array() -- 经对象中的一条数据匹配成索引和关联数组(数字、字段下标各一种),指针下移一条
mysqli_fetch_all() -- 匹配所有
mysqli_num_rows() -- 返回的行数
mysqli_num_fields() -- 返回的列数
mysqli_free_result() -- 销毁结果集合
mysqli_close() -- 关闭连接
示例:
<?php
# 连接数据库
$conn = mysqli_connect('localhost','root','Admin123','junglezt','3306');
# 设置字符集
mysqli_set_charset($conn,'utf8');
# 进行 SQL 查询
$sql = 'select * from user';
$result = mysqli_query($conn,$sql);
# 取出查询中每行的值
// $row1 = mysqli_fetch_assoc($result);
// $row2 = mysqli_fetch_row($result);
// $row3 = mysqli_fetch_array($result);
// echo "<pre>";
// print_r($row1);
// print_r($row2);
// print_r($row3);
# 查询返回的行数、列数
// $row = mysqli_num_rows($result);
// $list = mysqli_num_fields($result);
// echo $row,"--",$list;
循环查询中所有字段的内容
foreach(mysqli_fetch_all($result) as $resp){
echo "ID : ".$resp[0]."\n";
echo "usernmae : ".$resp[1]."\n";
echo "password : ".$resp[2]."\n";
echo "\n";
}
# 销毁结果集合
// mysqli_free_result($result);
// var_dump($result);
# 断开连接
// mysqli_close($conn);
?>
数据库操作之开个玩笑的用户后台管理
conn.php 数据库连接文件
<?php
# 连接数据库
$conn = mysqli_connect('localhost','root','Admin123','junglezt','3306') or die('数据库连接失败'.mysqli_connect_error());
?>
index.php:主页
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>用户管理</title>
<style type="text/css">
body{
background-image: url('./img/bg.jpg');
background-size: 100% ;
background-repeat: no-repeat;
}
table{
ackground-color: #00000060;
border: 1px solid #000000;
margin: auto;
margin-top: 10%;
}
td{
padding: 5px;
border: 1px solid #000000;
}
.adduser{
margin-top: 10px;
margin-left: 510px;
font-size: 20px;
}
</style>
</head>
<body>
<?php
include "./inc/conn.php";
$sql = 'select * from user';
$result = mysqli_query($conn,$sql);
$link = mysqli_fetch_all($result);
?>
<table>
<th>ID</th> <th>用户名</th> <th>密码</th> <th>修改</th> <th>删除</th>
<?php foreach($link as $resp):?>
<tr>
<td><?php echo $resp[0]?></td>
<td><?php echo $resp[1]?></td>
<td><?php echo $resp[2]?></td>
<td><a href="./update.php?user=<?php echo $resp[1]?>"><input type="submit" name="submit" value="修改"></a></td>
<td><a onclick="if(window.confirm('确定要删除吗?'))location.href='./del.php?id=<?php echo $resp[0]?>'"><input type="submit" name="submit" value="删除"></a></td>
</tr>
<?php endforeach;?>
</table>
<div>
<a href="./insert.php"><input type="submit" name="submit" value="创建" class="adduser"></a>
</div>
</body>
</html>
修改页面:update.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>修改用户信息</title>
</head>
<body>
<?php
include "./inc/conn.php";
$user = $_GET['user'];
echo "当前用户是:{$user}";
?>
<form action="" method="POST">
新的用户名:<input type="text" name="new_name"><br>
旧密码:<input type="password" name="password"><br>
新密码:<input type="password" name="new_pass"><br>
<input type="submit" name="修改">
</form>
<?php
$new_name = $_POST['new_name'];
$new_pass = $_POST['new_pass'];
$result = mysqli_query($conn,"select * from user where username='{$user}'");
@$row_info = mysqli_fetch_row($result);
if ($row_info[1] == $user){
if (!empty($new_name) && !empty($new_pass)){
if ($row_info[2] === $_POST['password']){
$sql = "update user set username='{$new_name}',password='{$new_pass}' where username='{$user}'";
$resp = mysqli_query($conn,$sql);
if ($resp){
echo "用户名、密码更改成功";
}else{
echo "用户名、密码更改失败";
}
}else{
echo "旧密码不一致";
}
}else{
echo "新用户名、密码不能为空";
}
}else{
echo "未找到该用户";
}
?>
</body>
</html>
删除页面:del.php
<?php
include "./inc/conn.php";
$id = $_GET['id'];
$sql = "delete from user where id={$id}";
$result = mysqli_query($conn,$sql);
if ($reuslt == NULL){
header("location:index.php");
}else{
echo "删除失败";
}
?>
创建用户页面: insert.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>创建用户</title>
</head>
<body>
<form action="insert.php" method="POST">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password_new"><br>
确认密码:<input type="password" name="password_again"><br>
<input type="submit" name="submit" value="创建用户">
</form>
<?php
include "./inc/conn.php";
if (!empty($_POST['submit'])){
# 获取输入的用户名和密码
$user = $_POST['username'];
$pass = $_POST['password_new'];
# 判断两次输入的密码是否一次
if (empty($pass)){
echo "密码不能为空";
}elseif($pass === $_POST['password_again']){
# 查询是否存在创建的用户
$select = mysqli_query($conn,"select * from user where username='{$user}'");
@$select_row = mysqli_num_rows($select);
# 判断是否已经创建
if ($select_row){
echo "{$user}已存在";
}else{
# 获取当前的一共有几个用户
$result = mysqli_query($conn,'select * from user');
$row = mysqli_num_rows($result);
# 进行用户创建
$sql = "insert into user values ({$row},'{$user}','{$pass}')";
$resp = mysqli_query($conn,$sql);
# 如果SQL语句执行成功,用户创建成功
if ($resp){
echo "用户创建成功";
}else{
echo "用户创建失败";
}
}
}else{
echo "两次输入的密码不一致";
}
}else{
echo "请输入需要创建的用户和密码";
}
?>
</body>
</html>
这里是有SQL注入的,以后可以用来SQL注入的防护,和HTML加CSS的美化的练习,主要的功能都实现了。