码迷,mamicode.com
首页 > Web开发 > 详细

自学PHP我也不知道第几天了。。。

时间:2017-08-30 16:38:18      阅读:169      评论:0      收藏:0      [点我收藏+]

标签: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我也不知道第几天了。。。

标签:delete   bsp   block   数据   sso   成功   语句   sel   strong   

原文地址:http://www.cnblogs.com/KennyHito/p/7453589.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!