<?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", "byzy", "123456");
//默认的 MySQL的类,其属性与方法见手册
if ($mysqli->connect_error) { //connect_error为属性,报错
die("数据库连接失败:" . $mysqli->connect_errno . "--" . $mysqli->connect_error);
// connect_errno:错误编号
}
$mysqli->select_db("byzy_cc"); //选择数据库
// $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](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 8 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
博客小陈
- 粉丝: 15
- 资源: 20
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)