2015-01-13 2 views
-1

php/mysql에서 스크립트에 문제가 있습니다. 여기 서버에 표시되는 오류입니다 :php 및 mysql의 치명적인 오류

Fatal error: Uncaught exception 'PDOException' with message '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 'if exists (select * from notificacoes where uid in() order by id desc' at line 1' in C:\wamp\www\bigui\classes\Notificacoes.class.php on line 57 

은 여기 내 PHP 코드 : MySQL의에서

static function listar(){ 

      $strIdAmigos = Amizade::$strIdAmigos; 

      $query = self::getConn()->query('select * from notificacoes where uid in ('.$strIdAmigos.') order by id desc'); 

      return $query->fetchAll(PDO::FETCH_ASSOC); 
     } 

내 테이블이없는 값, 비어 있습니다. 내가 값을 삽입하면 오류가 사라지고 모든 것이 잘됩니다. 어떤 도움?

+2

'$ strIdAmigos'의 값은 무엇입니까? 어떻게 계산됩니까? 오류 때문에 비어있는 것 같습니다. – Mureinik

+0

[MySQL에서 예약어를 테이블 또는 열 이름으로 사용하기 때문에 구문 오류가 발생할 수 있습니다] (http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word -as-a-table-or-column-name-in-mysql) –

답변

1

$strIdAmigos이 비어 있으면 구문 오류가 발생합니다.

이 쿼리를 실행하기 전에 $strIdAmigos 값이 비어 있는지 여부를 확인해야이 문제가 발생하지 않습니다. 필요한 경우 값을 이스케이프하는 것을 잊지 마십시오.

+0

이렇게하면됩니다. $ strIdAmigos = Amizade :: $ strIdAmigos; \t \t \t $ query = 'not * from notificacoes from ('. $ strIdAmigos. ') uid in id desc'); – user2701811

+0

wooah, 무엇이'$ strIdAmigos = Amizade :: $ strIdAmigos;'입니까? 의견을 편집하는 대신 질문을 편집하여 코드를 게시 할 수 있습니다. – Raptor

0

$strIdAmigos 변수를 사용하지 않고 쿼리를 실행하면 오류가 발생합니다.

시도 쿼리를 실행하기 전에, 초기화 및/또는 변수 $ strIdAmigos을 확인 : $strIdAmigos = "0" 경우 empty($strIdAmigos) 여전히 true로 평가하고, 따라서, 쿼리를 실행하지 것이다

$strIdAmigos = ""; 

if (empty($strIdAmigos)) { 
    /* Uh oh, throw an error */ 
} else { 
    $query = self::getConn()->query('select * from notificacoes where uid in ('.$strIdAmigos.') order by id desc'); 
} 

참고.