Implementation features:
1. Implement the app side to return JSON data;
2. Realize the function of adding and deleting data;
<?php
echo "Object-oriented style, connect database case <br>";
IF (!empty ($_post)) {
$isPan = $_post["type"];//used to determine additions and deletions
if ($isPan = = "Select") {
Inquire
$select 1 = new father;
$select 1->selectdata ();
}elseif ($isPan = = "Insert") {
Increase
$stuName = $_post["Stuname"];
$stuAge = $_post["Stuage"];
$stuHeight = $_post["Stuheight"];
$select 1 = new father;
$select 1->insertdata ($stuName, $stuAge, $stuHeight);
}elseif ($isPan = = "Update") {
Update
$stuName = $_post["Stuname"];
$stuAge = $_post["Stuage"];
$stuHeight = $_post["Stuheight"];
$select 1 = new father;
$select 1->updatedata ($stuName, $stuAge, $stuHeight);
}elseif ($isPan = = "Delete") {
Delete
$stuName = $_post["Stuname"];
$select 1 = new father;
$select 1->deletedata ($stuName);
}
}else{
$singleJson = Array ("Data" =>array (), "result" = "please use POST request!");
$jsonS = Json_encode ($singleJson);
Echo $jsonS;
}
Class father{
//Create connection
function base () {
$conn = new mysqli ("localhost", "root", "", "php_test");
Var_dump ($conn);
//Read data from MySQL database, set encoding
Mysqli_query ($conn, "set NAMES utf8mb4");
Determine if the connection database is successful
if ($conn->connect_error) {
Die ("Connection failed:". $conn->connect_error);
}
return $conn;
}
//query <--------> query database Data
Function Selectdata () {
$conn = $this- >base ();
$sql = "SELECT * from Zrzf_student";
//Execute SQL statement
//??????????: $conn->query ($sql); Here query is the method for the instance object;
$result = $conn->query ($sql);
if ($result->num_rows > 0) {
$dataArr = array ();
The FETCH_ASSOC result is an associative array
while ($row = $result->fetch_assoc ()) {
//output Data
$result 1= (array) $row;
$DATAARR [] = $result 1;
}
//Var_dump ($DATAARR);
$backJson = Array ("data" = = $DATAARR, "result" = "Congratulations, query Success!");
$jsonS = Json_encode ($backJson);
Echo $jsonS;
Die;
}else {
$backJson = array ("Data" =>array (), "result" = "database is 0, no query data!");
$jsonS = Json_encode ($backJson);
Echo $jsonS;
Die;
}
$conn->close ();
}
//Add <--------> Insert database Data
function InsertData ($stuName, $stuAge, $ Stuheight) {
$conn = $this->base ();
$str = ";
$sql 1 = "INSERT into Zrzf_student (Stuname, Stuage, Stuheight) VALUES ('". $stuName. "', $stuAge, $stuHeight)";
if ($conn->query ($sql 1) = = = TRUE) {
$str = "Increase record success!";
} else {
$str = "Failed to add record! Error: ". $sql 1." <br> ". $conn->error;
}
$backJson = Array ("Data" =>array (), "result" = = $str);
$jsonS = Json_encode ($backJson);
Echo $jsonS;
Die;
$conn->close ();
}
//delete <--------> Delete database data
function DeleteData ($stuName) {
$str = ' ;
$conn = $this->base ();
$sql 2 = "DELETE from zrzf_student WHERE stuname= '". $stuName. "'";
if ($conn->query ($sql 2) ==true) {
$str = "Delete record succeeded!";
}else{
$str = "failed to delete record! Error: ". $sql 2." <br> ". $conn->error;
}
$backJson = Array ("Data" =>array (), "result" = = $str);
$jsonS = Json_encode ($backJson);
Echo $jsonS;
Die;
$conn->close ();
}
Update <--------> Update database data
function UpdateData ($stuName, $stuAge, $stuHeight) {
$conn = $this->base ();
$str = ";
$sql 3 = "UPDATE zrzf_student SET stuage= $stuAge, stuheight= $stuHeight WHERE stuname= '". $stuName. "'";
if ($conn->query ($sql 3) ==true) {
$STR = "Update record succeeded!";
}else{
$str = "Failed to update record! Error: ". $sql 3." <br> ". $conn->error;
}
$backJson = Array ("Data" =>array (), "result" = = $STR);
$jsonS = Json_encode ($backJson);
Echo $jsonS;
Die
$conn->close ();
}
}
Self-taught PHP I do not know the first few days ...