2013-10-23 1 views
0

나는 PHP의 PDO를 사용하여 데이터베이스를 쿼리하여 사이트의 RESTful 백엔드를 구축하기 위해 슬림 프레임 워크를 사용하고 있습니다. PDO는 작은 따옴표 내 바인딩 된 매개 변수를 둘러싼되는 것처럼PDO 바운드 매개 변수에 작은 따옴표 추가하기

string(206) "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''10'' at line 1"`

것 같습니다 : 나는 준비된 명령문에 매개 변수를 바인딩하기 위해 노력하고있어 때, 나는 다음과 같은 오류가 발생합니다. 내 경로 코드는 다음과 같습니다.

$app->get("/members/next10/:offset", function($offset) use($app) { 
    $app->response()->header('Content-Type', 'application/json'); 

    try { 
     $pdo = connectToDatabase(); 

     $query = $pdo->prepare("SELECT * FROM `members` LIMIT 10 OFFSET :offset"); 
     $query->bindParam(":offset", $offset, PDO::PARAM_INT); 
     $query->execute(); 

     $result = $query->fetchAll(PDO::FETCH_ASSOC); 

     if (!empty($result)) { 
      echo json_encode($result); 
     } else { 
      $app->response()->status(404); 
     } 
    } catch (PDOException $d) { 
     $app->response()->status(500); 
     var_dump($d->getMessage()); 
    } 
}); 

내가 누락 된 항목이 있습니까? 나는 URL에서 :offset을 움켜 잡으려고 시도하고 바인딩하기 전에 정수로 캐스팅하여 $offset을 지정했지만 차이는 없습니다.

+0

가능한 중복 http://stackoverflow.com/questions/5508993/pdo-limit-and-offset : 같은

경로 보인다 – fortune

답변

0

가능한 해결책 : param을 정수로 적절하게 형변환하십시오. 메서드의 첫 번째 줄에 $offset = (int) $offset;을 사용하면 작동합니다. 이전에 나는 $query->bindParam() 방법으로 캐스트를하려고했습니다.

$app->get("/members/next10/:offset", function($offset) use($app) { 
    $offset = (int) $offset; 

    $app->response()->header('Content-Type', 'application/json'); 

    try { 
     $pdo = connectToDatabase(); 

     $query = $pdo->prepare("SELECT * FROM `members` LIMIT 10 OFFSET :offset"); 
     $query->bindParam(":offset", $offset, PDO::PARAM_INT); 
     $query->execute(); 

     $result = $query->fetchAll(PDO::FETCH_ASSOC); 

     if (!empty($result)) { 
      echo json_encode($result); 
     } else { 
      $app->response()->status(404); 
     } 
    } catch (PDOException $d) { 
     $app->response()->status(500); 
     var_dump($d->getMessage()); 
    } 
});