json从php返回select查询的空数组,没有任何错误

cunj1qz1  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(178)

我已经使用post from angular component将值传递到php,并且能够查看传递的输入值,但是在select query中使用输入时无法获取任何记录php代码:

$json    =  file_get_contents('php://input');
    $obj     =  json_decode($json);

    $selectedDept = filter_var($obj->selectyear, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
    $selectedYear = filter_var($obj->selectDepart, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);

   $searchdata = array();

    try {
      $stmt     = $pdo->prepare('SELECT * FROM circular_data WHERE YEAR(Circular_date)=:selectyear AND Circular_department=:selectDepart');
            $stmt->bindParam(':selectyear', $selectedYear);
            $stmt->bindParam(':selectDepart', $selectedDept);
            $stmt->execute();

      while($row  = $stmt->fetch(PDO::FETCH_OBJ))
      {
         // Assign each row of data to associative array
         $searchdata[] = $row;
      }

      echo json_encode($searchdata);
   }

   catch(PDOException $e)
   {
      echo $e->getMessage();
   }

使用post请求传递输入的组件类代码:

this.selectedData = {
    selectyear: this.selectedYear,
    selectDepart: this.selectedDepartment
  }

  let selectData = this.selectedData;

  this.http.post('http://localhost:8080/dbcon/retrieve-searchdata.php', selectData, httpOptions)
  .subscribe((searchdata : any) =>
  {
      console.log(searchdata);
  },
    err => {
      console.log("Error occured", err);
    }
  );

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题