標籤:delete bsp block 資料 sso 成功 語句 sel strong
實現功能:
1.實現app端返回json資料;
2.實現資料的增刪改查功能;
<?php
// echo "物件導向風格,串連資料庫案例<br>";
if (!empty($_POST)) {
$isPan = $_POST["type"];//用來判斷增刪改查
if ($isPan=="select") {
//查詢
$select1 = new father;
$select1->selectData();
}elseif ($isPan=="insert") {
//增加
$stuName = $_POST["stuName"];
$stuAge = $_POST["stuAge"];
$stuHeight = $_POST["stuHeight"];
$select1 = new father;
$select1->insertData($stuName,$stuAge,$stuHeight);
}elseif ($isPan=="update") {
//更新
$stuName = $_POST["stuName"];
$stuAge = $_POST["stuAge"];
$stuHeight = $_POST["stuHeight"];
$select1 = new father;
$select1->updateData($stuName,$stuAge,$stuHeight);
}elseif ($isPan=="delete") {
//刪除
$stuName = $_POST["stuName"];
$select1 = new father;
$select1->deleteData($stuName);
}
}else{
$singleJson = array("data"=>array(),"result"=>"請使用POST請求!");
$jsonS = json_encode($singleJson);
echo $jsonS;
}
class father{
// 建立串連
function base(){
$conn = new mysqli("localhost", "root", "", "php_test");
//var_dump($conn);
//從MySQL資料庫讀取資料,設定編碼
mysqli_query($conn, "SET NAMES utf8mb4");
//判斷串連資料庫是否成功
if ($conn->connect_error) {
die("串連失敗:".$conn->connect_error);
}
return $conn;
}
//查詢<-------->查詢資料庫資料
function selectData(){
$conn = $this->base();
$sql = "SELECT * FROM zrzf_student";
//執行sql語句
//??????????: $conn->query($sql);這裡query是方法,執行個體對象的方法;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$dataArr = array();
//fetch_assoc 結果是關聯陣列
while($row = $result->fetch_assoc()) {
// 輸出資料
$result1= (array)$row;
$dataArr[] = $result1;
}
// var_dump($dataArr);
$backJson = array("data"=>$dataArr,"result"=>‘恭喜你,查詢成功!‘);
$jsonS = json_encode($backJson);
echo $jsonS;
die;
}else {
$backJson = array("data"=>array(),"result"=>‘資料庫為0,沒有查詢資料!‘);
$jsonS = json_encode($backJson);
echo $jsonS;
die;
}
$conn->close();
}
//增加<-------->插入資料庫資料
function insertData($stuName, $stuAge, $stuHeight){
$conn = $this->base();
$str = ‘‘;
$sql1 = "INSERT INTO zrzf_student (stuName, stuAge, stuHeight) VALUES (‘".$stuName."‘, $stuAge, $stuHeight)";
if ($conn->query($sql1) === TRUE) {
$str = "增加記錄成功!";
} else {
$str = "增加記錄失敗!Error:".$sql1."<br>".$conn->error;
}
$backJson = array("data"=>array(),"result"=>$str);
$jsonS = json_encode($backJson);
echo $jsonS;
die;
$conn->close();
}
//刪除<-------->刪除資料庫資料
function deleteData($stuName){
$str = ‘‘;
$conn = $this->base();
$sql2 = "DELETE FROM zrzf_student WHERE stuName=‘".$stuName."‘";
if($conn->query($sql2)==TRUE){
$str = "刪除記錄成功!";
}else{
$str = "刪除記錄失敗!Error:".$sql2."<br>".$conn->error;
}
$backJson = array("data"=>array(),"result"=>$str);
$jsonS = json_encode($backJson);
echo $jsonS;
die;
$conn->close();
}
//更新<-------->更新資料庫資料
function updateData($stuName,$stuAge,$stuHeight){
$conn = $this->base();
$str = ‘‘;
$sql3 = "UPDATE zrzf_student SET stuAge=$stuAge,stuHeight=$stuHeight WHERE stuName=‘".$stuName."‘";
if($conn->query($sql3)==TRUE){
$str = "更新記錄成功!";
}else{
$str = "更新記錄失敗!Error:".$sql3."<br>".$conn->error;
}
$backJson = array("data"=>array(),"result"=>$str);
$jsonS = json_encode($backJson);
echo $jsonS;
die;
$conn->close();
}
}
自學PHP我也不知道第幾天了。。。