<?php
/**
* 作者:技术宅妞
* QQ:522605223
* 原创作品请保留版权注释
*/
header('Content-Type:application/json; charset=utf-8');
$fun = $_GET["fun"];
$id = $_GET["id"];
$title = $_GET["title"];
$content = $_GET["content"];
$name = $_GET["auth"];
$txt = $_GET["txt"];
switch ($fun) {
case "getbook":
getbook($txt);
break;
case "setbook":
setbook($title, $content, $name);
break;
case "delbook":
delbook($id);
break;
case "upbook":
upbook($id, $title, $content, $name);
break;
default:
$res = array("code" => 1, "msg" => "方法不存在");
echo json_encode($res);
}
function coon($sql, $type = 1)
{
// $host = "localhost"; //定义默认连接方式
// $zhang = "byzy"; //定义默认用户名
// $mi = "123456"; //定义默认的密码
// $dbname = "byzy_cc"; //定义默认的数据库名
// $db = new mysqli($host, $zhang, $mi, $dbname);
// $r = $db->query($sql);
// if ($type == "1") {
// return $r->fetch_all(); //查询语句,返回数组.执行sql的返回方式是all,也可以换成row
// } else {
// return $r;
// }
//第一步:创建数据连接对象
$mysqli = new MySQLi("localhost", "ceshi", "ceshi");
//默认的 MySQL的类,其属性与方法见手册
if ($mysqli->connect_error) { //connect_error为属性,报错
die("数据库连接失败:" . $mysqli->connect_errno . "--" . $mysqli->connect_error);
// connect_errno:错误编号
}
$mysqli->select_db("ceshi"); //选择数据库
// $mysqli->query("set names 'GBK'");
//第二步:数据库查询,代码如下
// $sql = "select title,contents from news limit 5,5";
//第三步:执行,显示结果
$res = $mysqli->query($sql) or die($mysqli->error);
// 返回值 $res 为资源类型(获取到结果的资源类型)
if ($type == 1) {
$arr = [];
while ($row = $res->fetch_assoc()) {
array_push($arr, $row);
}
return $arr;
$res->free();
} else {
return $res;
}
//第四步,释放结果集,关闭连接
// $res->free();
$mysqli->close();
}
function getbook($txt = "")
{
$sql = "";
if ($txt) {
$sql = "select * from book where title LIKE '" . '%' . $txt .'%'. "'";
} else {
$sql = "select * from book";
}
$arr = coon($sql);
if (count($arr) > 0) {
$res = array("code" => 0, "data" => $arr);
echo json_encode($res);
} else {
$res = array("code" => 1, "msg" => "暂无数据");
echo json_encode($res);
}
}
function setbook($title, $content, $name)
{
$sql = "INSERT INTO book ( title, content,auth ) VALUES( '" . $title . "', '" . $content . "','" . $name . "' );";
$arr = coon($sql, 0);
if ($arr) {
$res = array("code" => 0, "msg" => "新增成功!");
echo json_encode($res);
} else {
$res = array("code" => 1, "msg" => "新增失败!");
echo json_encode($res);
}
}
function upbook($id, $title, $content, $name)
{
$sql = "UPDATE book SET title='" . $title . "',content='" . $content . "',auth='" . $name . "' where id = $id";
$res = coon($sql, 0);
if ($res) {
$res = array("code" => 0, "msg" => "修改成功");
echo json_encode($res);
} else {
$res = array("code" => 1, "msg" => "修改失败");
echo json_encode($res);
}
}
function delbook($id)
{
$sql = "DELETE FROM book where id=$id";
$res = coon($sql, 0);
if ($res) {
$res = array("code" => 0, "msg" => "删除成功");
echo json_encode($res);
} else {
$res = array("code" => 1, "msg" => "删除失败");
echo json_encode($res);
}
}
图书库毕业设计网页源码.zip
版权申诉
20 浏览量
2023-01-16
14:24:31
上传
评论
收藏 6KB ZIP 举报
智慧浩海
- 粉丝: 1w+
- 资源: 5203