mysqli数据库操作;
1.1、数据库操作代码
<?php
namespace mysql;
use mysqli;
$arguments=require 'config.php';
// mysqli连接数据库需要的参数:主机名、用户名、密码、默认数据库
/*
*@var string $host
*@var string $username
*@var string $password
*@var string $dbname
*/
extract($arguments);
// 连接数据库
$db=new mysqli($host,$username,$password,$dbname);
// 测试是否连接成功;
// echo $db->connect_errno;
$stmt=$db->stmt_init();//初始化数据库等待查询
$action=strtolower($_GET['action']);
// echo $action;
switch($action){
case 'select':
$sql='SELECT * FROM `goods` WHERE `id`=?';
$stmt->prepare($sql);
$id=$_POST['id'];
$stmt->bind_param('i',$id);
$stmt->execute();
$res=$stmt->get_result();
$data=$res->fetch_array(MYSQLI_ASSOC);
// print_r($data);
extract($data);
$datetime=date('Y-m-d H:i:s',$datetime+8*3600);
printf("<li>编号: %s , 产品: %s, 价格: %s ,描述:%s, 创建时间:%s</li>", $id,$name,$price,$details,$datetime);
$res->free_result();//释放结果集
break;
case 'insert':
$sql='INSERT `goods` SET `name`=?,`price`=?,`details`=?,`datetime`=?';
$stmt->prepare($sql);
// $data=['富豫369','50','红轴,大穗,矮杆,耐高温不华丽',1589122280];
// list($name,$price,$details,$datetime)=$data;
$name=$_POST['name'];
$price=$_POST['price'];
$details=$_POST['details'];
$datetime=time();
// echo $name,$price,$details,$datetime;
$stmt->bind_param('sssi',$name,$price,$details,$datetime);
$stmt->execute();
if($stmt->affected_rows===1){
echo '添加成功,新产品id:'.$stmt->insert_id;
}else{
echo '添加失败!原因:'.$stmt->error;
}
break;
case 'update':
$sql="UPDATE `goods` SET `price`=?,`details`=? WHERE id=? ";
$stmt->prepare($sql);
$id=intval($_POST['id']);
$price=$_POST['price'];
$details=$_POST['details'];
// var_dump($id);
// echo $price,$details;
$stmt->bind_param('ssi',$price,$details,$id);
$stmt->execute();
if($stmt->affected_rows===1){
echo '更新成功';
}else{
echo '更新失败'.$stmt->error;
}
break;
case 'delete':
$sql='DELETE FROM `goods` WHERE `id`=?';
$id=intval($_POST['id']);
$stmt->prepare($sql);
$stmt->bind_param('i',$id);
$stmt->execute();
if($stmt->affected_rows===1){
echo '删除成功';
}else{
echo '删除失败'.$stmt->error;
}
break;
default:
echo '非法操作';
}
$stmt->close();
1.2操作页面代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>数据库操作案例</title>
</head>
<body>
<h1>数据库操作案例</h1>
<hr>
<div>
<form action="mysql.php?action=insert" method='POST'>
<span>增加:</span>
<label for="">产品名字:</label><input type="text" name="name" required placeholder="富豫369">
<label for="">产品价格:</label><input type="text" name="price" required placeholder="60">
<label for="">产品描述:</label><input type="text" name="details" required placeholder="红轴、大穗">
<!-- <label for="">创建日期:</label></label><input type="date" name="datetime" required> -->
<button>增加</button>
</form>
</div>
<div>
<form action="mysql.php?action=update" method="POST">
<span>更新:</span>
<label for="">产品:ID</label><input type="number" name="id" required placeholder="输入正整数">
<label for="">产品价格:</label><input type="text" name='price' required placeholder="60">
<label for="">产品描述:</label><input type="text" name="details" required placeholder="红轴、大穗">
<button>更新</button>
</form>
</div>
<div>
<form action="mysql.php?action=select" method="POST">
<span>查询:</span>
<label for="">产品:ID</label><input type="number" name="id" required placeholder="输入正整数">
<button>查询</button>
</form>
</div>
<div>
<form action="mysql.php?action=delete" method="POST">
<span>删除:</span>
<label for="">产品:ID</label><input type="number" name="id" required placeholder="输入正整数">
<button>删除</button>
</form>
</div>
</body>
</html>
2、运行结果
总结
1、数据库连接步骤:
free_result();1、mysqli数据库连接:$db=new mysqli(主机名,用户名,用户密码,默认数据库);
2、初始数据库:$stmt=$db->stmt_init();
3、准备数据库操作语句:增删改查(sql);
4、预处理sql语句:$stmt->prepare($sql);
5、绑定处理语句参数:$stmt->bindparam();
6、执行sql语句:$stmt->excute();
8、断开数据库连接:$stmt->close();(如果是查询数据库需要释放结果集:free_result();)
2、mysqli操作中常见函数:
affected_rows():操作数据库收到影响的记录
insert_id():插入记录的ID;
$stmt->error;执行最后以一个语句的返回的错误信息;
data_seek();指针函数;