周末有空弄了下数据库操作,实现了增删查改的基本操作,记录一下。
一、增
<?php
$servername = "100.0.0.0";
$username = "zjwdb_xxxx";
$password = "xxxxx";
$dbname = "zjwdb_xxxxx";
$name = $_GET['name'];
$age = $_GET['age'];
$type = $_GET['type'];
if ($name == "") {
die('{"code":101,"msg":"请输入name"}');
}
if ($age == "") {
die('{"code":102,"msg":"请输入age"}');
}
if ($type == "") {
die('{"code":103,"msg":"请输入type"}');
}
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
mysqli_query($conn, "set names 'UTF8'");
// 检测连接
if (!$conn) {
die('{"code":400,"msg":"'.mysqli_connect_error($conn).'"}');
}
$sql = "INSERT INTO userInfo(name, age, type) VALUES (\"$name\",$age,\"$type\")";
if (mysqli_query($conn, $sql)) {
echo '{"code":200,"msg":"添加成功"}';
} else {
echo '{"code":400,"msg":"'.mysqli_error($conn).'"}';
}
mysqli_close($conn);
?>
二、删
<?php
$servername = "100.0.0.0";
$username = "zjwdb_xxxxx";
$password = "xxxxx";
$dbname = "zjwdb_xxxx";
$id = $_GET['id'];
if ($id == "") {
die('{"code":101,"msg":"请输入删除的id"}');
}
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
mysqli_query($conn, "set names 'UTF8'");
// 检测连接
if (!$conn) {
die('{"code":400,"msg":"'.mysqli_connect_error($conn).'"}');
}
$sql = "DELETE FROM userInfo WHERE id=$id";
if (mysqli_query($conn, $sql)) {
echo '{"code":200,"msg":"删除成功"}';
} else {
echo '{"code":400,"msg":"'.mysqli_error($conn).'"}';
}
mysqli_close($conn);
?>
三、查
<?php
$servername = "100.0.0.0";
$username = "zjwdb_xxxx";
$password = "xxxx";
$dbname = "zjwdb_xxxxx";
$json = "";
$data = array();
class User {
public $id;
public $name;
public $age;
public $type;
}
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
mysqli_query($conn, "set names 'UTF8'");
// Check connection
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
$sql = "SELECT * FROM userInfo";
$result = $conn->query($sql);
if ($result) {
//echo "查询成功";
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$user = new User();
$user->id = $row["id"];
$user->name = $row["name"];
$user->age = $row["age"];
$user->type = $row["type"];
$data[] = $user;
}
$json = json_encode($data); //把数据转换为JSON数据.
echo $json;
} else {
echo "[]";
}
mysqli_close($conn);
?>
四、改
<?php
$servername = "100.0.0.0";
$username = "zjwdb_xxxx";
$password = "xxxx";
$dbname = "zjwdb_xxxx";
$id = $_GET['id'];
$name = $_GET['name'];
$age = $_GET['age'];
$type = $_GET['type'];
if ($id == "") {
die('{"code":101,"msg":"请输入修改的id"}');
}
if ($name == "") {
die('{"code":101,"msg":"请输入name"}');
}
if ($age == "") {
die('{"code":102,"msg":"请输入age"}');
}
if ($type == "") {
die('{"code":103,"msg":"请输入type"}');
}
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
mysqli_query($conn, "set names 'UTF8'");
// 检测连接
if (!$conn) {
die('{"code":400,"msg":"'.mysqli_connect_error($conn).'"}');
}
$sql = "UPDATE userInfo SET name=\"$name\",age=\"$age\",type=\"$type\" WHERE id=$id";
if (mysqli_query($conn, $sql)) {
echo '{"code":200,"msg":"修改成功"}';
} else {
echo '{"code":400,"msg":"'.mysqli_error($conn).'"}';
}
mysqli_close($conn);
?>
API测试
1、增加用户信息:http://linghit.vip/user/add.php?name=景天&age=24&type=刺客
2、删掉用户信息:http://linghit.vip/user/del.php?id=5
3、查询用户信息:http://linghit.vip/user/check.php
4、修改用户信息:http://linghit.vip/user/update.php?name=景天&age=24&type=刺客&id=1