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

Posted 海豚的信笺

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自学PHP我也不知道第几天了。。。相关的知识,希望对你有一定的参考价值。

实现功能:

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我也不知道第几天了。。。的主要内容,如果未能解决你的问题,请参考以下文章

零基础自学JAVASE Part1

自学编程真的很无力吗?不,完全可以相信自己。肝 5000+字自述java自学

自学编程需要注意什么?

学PHP自学还是培训?

选择PHP是自学还是去PHP培训班的看这里

选择PHP是自学还是去PHP培训班的看这里