>  Q&A  >  본문

PHP에서 여러 필터를 사용하여 REST API GET 메서드 만들기

매개변수 ID를 사용하여 PHP에서 REST API GET 메서드를 만들었습니다. http://localhost:8012/REST-API/items/expenses?id=2. 이것은 잘 작동하지만 http://localhost:8012/REST-API/items/expenses?id=2&year=2022를 사용하여 데이터를 가져오고 싶습니다. 저는 PHP를 처음 접했고 모든 가능성을 시도했지만 올바른 응답을 얻지 못했습니다. 아래는 코드입니다

/class 파일

의 Expenses.php 으아아아

readexpenses.php

으아아아

.htaccess

<?php
class Expenses{   
    private $expensesTable = "expenses";      
    public $id;
    public $month;
    private $amount;
    public $year;
    
    public function __construct($db){
        $this->conn = $db;
    }   

    function expenses(){    
        if($this->year) {
            $stmt = $this->conn->prepare("SELECT * FROM ".$this->expensesTable." WHERE year = ?");
            $stmt->bind_param("i", $this->year);                    
        } 
        else if($this->id) {
            $stmt = $this->conn->prepare("SELECT * FROM ".$this->expensesTable." WHERE id = ?");
            $stmt->bind_param("i", $this->id);                  
        }
        else if($this->id && $this->year) {
            $stmt = $this->conn->prepare("SELECT * FROM ".$this->expensesTable." WHERE id = ? AND year= ?");
            $stmt->bind_param("i", $this->id,$this->year);                  
        }

        else {
            $stmt = $this->conn->prepare("SELECT * FROM ".$this->expensesTable);        
        }       
        $stmt->execute();           
        $result = $stmt->get_result();      
        return $result; 
    }
}
?>

P粉937769356P粉937769356238일 전322

모든 응답(1)나는 대답할 것이다

  • P粉642919823

    P粉6429198232024-01-30 00:37:39

    댓글에서 말했듯이 그의 마지막 세 가지 규칙은 잘못되었습니다.

    으아악

    이 URL은 어떻게 되나요? http://example.com/expenses/2012 如何检测这是 idyear?

    다음 솔루션을 사용하는 것이 좋습니다:

    으아악

    그래서 .htaccess 파일은 다음과 같습니다: (마지막 세 가지 규칙만 변경함)

    으아악

    온라인 테스트:

    테스트해본 결과 모든 경우에 작동합니다. id /年id< /code>年份 , 아래에서 URL을 통과하는 것을 볼 수 있습니다

    PHP 조건도 변경해야 합니다

    으아악

    이 변경 없이는 ID와 연도가 설정된 시간에 도달하지 못할 것입니다

    회신하다
    0
  • 취소회신하다