php
php + wampserver启动后端
wampserver双击启动图标变绿表示启动成功
如果图标是橙色,打开任务管理器 --> 服务 --> 找到mysql关闭再重启wanmpserver即为绿色启动成功
启动成功后即可调用接口,调用时请求超时:Proxy error: Could not proxy request /appi/login.php from localhost:9528 to http://192.168.1.4:80 (ETIMEDOUT).
打开cmd --> ping http://192.168.1.4 检查网络连通性,如果没连接成功使用ipconfig查看ipv4地址
修改前端vue.config.js文件下的代理地址为本机ipv4地址,重新npm run dev才能生效
接口即可正常调用
php登录接口
php
<?php
// 登录
$servername = "localhost";
$username = "root";
// 密码内容
$password = "******";
// 数据库名
$dbname = "financial";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
if(file_get_contents("php://input")) {
$value = file_get_contents("php://input");
$username = json_decode($value)->username;
$password = json_decode($value)->password;
// $md5password = md5($password);
$sql = "SELECT password,id FROM login
WHERE username='$username'";
// $result = $conn->query($sql);
mysqli_options($conn, MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
$resultData = $result->fetch_assoc();
if ($resultData['password'] == $password) {
$data['token'] = 'this is token';
$data['userId'] = $resultData['id'];
$data['token'] = 'this is token';
$data['msg'] = '登录成功';
$response = array('data' => $data, 'code' => 200);
} else {
$response = array('msg' =>'密码不正确', 'code' =>500);
}
} else {
$response = array('msg' =>'用户名不正确', 'code' =>500);
}
// 获取输出的数据
// $data['result'] = $result->fetch_assoc();
// $response = array('data' => $data, 'code' => 200);
} else {
$response = array('msg' =>"没传参数", 'code' =>500);
}
echo json_encode($response);
?>
php注册接口
php
<?php
// 注册
$servername = "localhost";
$username = "root";
$password = "******";
// 数据库名
$dbname = "financial";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
if(file_get_contents("php://input")) {
$value = file_get_contents("php://input");
$username = json_decode($value)->username;
$password = json_decode($value)->password;
$md5password = md5($password);
$sql = "INSERT INTO login(username, password)
VALUES ('".$username."','".$md5password."')";
if ($conn->query($sql) === TRUE) {
$response = array('data' => $md5password, 'code' => 200);
} else {
$response = array('msg' =>$conn->error, 'code' =>500);
}
} else {
$response = array('msg' =>"没传参数", 'code' =>500);
}
echo json_encode($response);
?>
php创建接口
php
<?php
// 创建基金
$servername = "localhost";
$username = "root";
$password = "******";
// 数据库名
$dbname = "financial";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
if(file_get_contents("php://input")) {
$value = file_get_contents("php://input");
$id = json_decode($value)->userId;
$fundName = json_decode($value)->fundName;
$fundType = json_decode($value)->fundType;
$isBuy = json_decode($value)->isBuy;
$sql = "INSERT INTO fund_list(id, fund_name, fund_type, is_buy)
VALUES ('$id','$fundName','$fundType','$isBuy')";
if ($conn->query($sql) === TRUE) {
$response = array('data' => '创建成功', 'code' => 200);
} else {
$response = array('msg' =>$conn->error, 'code' =>500);
}
} else {
$response = array('msg' =>"没传参数", 'code' =>500);
}
echo json_encode($response);
?>
php列表接口(查询+分页)
php
<?php
// 获取基金列表
$servername = "localhost";
$username = "root";
$password = "******";
// 数据库名
$dbname = "financial";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->query('SET NAMES UTF8');
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
if(isset($_GET['userId'])) {
if (isset($_GET['page']) && isset($_GET['limit'])) {
$userId = $_GET['userId'];
$limit = $_GET['limit'];
$page = ($_GET['page'] - 1) * $limit;
$searchName = $_GET['searchName'];
$data['total'] = $conn->query("SELECT * FROM fund_list WHERE id = '$userId' and (fund_name LIKE '%$searchName%' or fund_type LIKE '%$searchName%')")->num_rows;
// $sql = "SELECT * FROM fund_list WHERE id = '$userId' ORDER BY 'fund_id' LIMIT $page,$limit";
$sql = "SELECT * FROM fund_list WHERE id = '$userId' and (fund_name LIKE '%$searchName%' or fund_type LIKE '%$searchName%') ORDER BY 'fund_id' LIMIT $page,$limit";
// $result = $conn->query($sql);
mysqli_options($conn, MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);
$result = mysqli_query($conn, $sql);
if (isset($result->num_rows)) {
$num = $result->num_rows;
} else {
$num = 0;
}
if ($num > 0) {
// if ($result->num_rows > 0) {
for($i=0; $i < $result->num_rows; $i++) {
$resultData = $result->fetch_assoc();
$row[$i]['fundId'] = $resultData['fund_id'];
$row[$i]['fundName'] = $resultData['fund_name'];
$row[$i]['fundType'] = $resultData['fund_type'];
$row[$i]['isBuy'] = $resultData['is_buy'];
}
// $row = mysqli_fetch_array($result);
$data['list'] = $row;
$response = array('data' => $data, 'code' => 200);
} else {
$data['list'] = array();
$response = array('data' => $data, 'code' => 200);
}
} else {
$response = array('msg' => "分页信息不正确", 'code' =>500);
}
} else {
$response = array('msg' => "当前用户不存在", 'code' =>500);
}
echo json_encode($response);
?>
php获取单个数据接口
php
<?php
// 获取单个基金内容
$servername = "localhost";
$username = "root";
$password = "******";
// 数据库名
$dbname = "financial";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
if(isset($_GET['fundId'])) {
$fundId = $_GET['fundId'];
$sql = "SELECT * FROM fund_list WHERE fund_id = '$fundId'";
mysqli_options($conn, MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);
$result = mysqli_query($conn, $sql);
// $result = $conn->query($sql);
if (isset($result->num_rows)) {
$num = $result->num_rows;
} else {
$num = 0;
}
if ($num > 0) {
$resultData = $result->fetch_assoc();
$row['fundId'] = $resultData['fund_id'];
$row['fundName'] = $resultData['fund_name'];
$row['fundType'] = $resultData['fund_type'];
$row['isBuy'] = $resultData['is_buy'];
// $row = mysqli_fetch_array($result);
$data = $row;
$response = array('data' => $data, 'code' => 200);
} else {
$response = array('msg' => "未查询到当前id下的信息", 'code' => 500);
}
} else {
$response = array('msg' => "未获取到当前基金id", 'code' =>500);
}
echo json_encode($response);
?>
php编辑单个数据接口
php
<?php
// 编辑基金
$servername = "localhost";
$username = "root";
$password = "******";
// 数据库名
$dbname = "financial";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
if(file_get_contents("php://input")) {
$value = file_get_contents("php://input");
$fundId = json_decode($value)->fundId;
$fundName = json_decode($value)->fundName;
$fundType = json_decode($value)->fundType;
$isBuy = json_decode($value)->isBuy;
$sql = "UPDATE fund_list set fund_name='$fundName', fund_type='$fundType', is_buy='$isBuy'
WHERE fund_id='$fundId'";
if ($conn->query($sql) === TRUE) {
$response = array('data' => '编辑成功', 'code' => 200);
} else {
$response = array('msg' =>$conn->error, 'code' =>500);
}
} else {
$response = array('msg' =>"没传参数", 'code' =>500);
}
echo json_encode($response);
?>
php删除单个数据接口
php
<?php
// 删除
$servername = "localhost";
$username = "root";
$password = "******";
// 数据库名
$dbname = "financial";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
if(file_get_contents("php://input")) {
$value = file_get_contents("php://input");
$fundId = json_decode($value)->fundId;
$query = mysqli_query($conn,"DELETE FROM fund_list WHERE fund_id ='$fundId'");
$queryChareList = mysqli_query($conn,"DELETE FROM note_list WHERE fund_id ='$fundId'");
$queryCharts = mysqli_query($conn,"DELETE FROM notes_charts WHERE fund_id ='$fundId'");
$queryBudget = mysqli_query($conn,"DELETE FROM budget_list WHERE fund_id ='$fundId'");
if ($query && $queryChareList && $queryCharts && $queryBudget) {
$response = array('data' => '删除成功', 'code' => 200);
} else {
$response = array('msg' =>$conn->error, 'code' =>500);
}
} else {
$response = array('msg' =>"没传参数", 'code' =>500);
}
echo json_encode($response);
?>
php 数据库内中文显示正常,调用接口乱码
解决方法如下:
<?php
// 获取基金列表
$servername = "localhost";
$username = "root";
$password = "******";
// 数据库名
$dbname = "financial";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 使用此方法设置utf-8格式中文就正常了
$conn->query('SET NAMES UTF8');
?>
php框架Laravel
php配置vscode插件
安装插件
PHP Debug
PHP IntelliSense