2012-11-29 3 views
2

PostgreSQL 8.4 DB와 통신하는 데 사용하는 다음 PDO 문과 같습니다.PDO로 빈 값을 보내면 오류가 발생합니다.

$st = $db -> prepare("INSERT INTO Saba.Betriebskosten (personalkosten) 
              VALUES(:kd_personalkosten)"); 
$st -> bindParam(':kd_personalkosten', $val['kd_personalkosten']); 

$ val [ 'kd_personalkosten']은 (는) 비어 있거나 null이거나 double 값을 포함합니다. 이/빈 null의 경우, 우리는 단지 빈 값을 삽입 할, 그러나 우리는 다음과 같은 오류가 나타납니다

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type double precision: ''; 

빈/널이와 호환되지 않습니다 빈 문자열로 변환되는 것을 의미 배정 밀도 필드. 이 오류를 우회하는 방법?

+1

var_dump ($ val [ 'kd_personalkosten'])는 무엇을 반환합니까? –

+0

'PostgreSQL 8.4'는 배정 밀도에 대해 NULL 값을 허용합니까? –

+0

@davidstrachan 필드가 NOT NULL로 정의되지 않은 경우 수행합니다. –

답변

3

SQL 쿼리에서 bindParam이 ""로 변환하는 값은 ""(빈 문자열)이고 personalkosten은 Double 유형이므로 오류가 발생합니다.

이 두 번 변환 빈 텍스트와 함께이 문제를 해결해야합니다

$st -> bindParam(':kd_personalkosten', (float) $val['kd_personalkosten']); 

당신이 정말로 변수가 다음이 작업을 수행해야 비어있을 때 NULL 값을 삽입하고 싶을 경우

$value = $val['kd_personalkosten']; 
if ($value === '' or $value === NULL) { 
    $st->bindValue(':kd_personalkosten', NULL, PDO::PARAM_NULL); // note the bindValue() instead of bindParam() 
} else { 
    $st->bindParam(':kd_personalkosten', $value); 
} 

소개 bindValue vs bindParam php 매뉴얼에서 :

bindParam()

Binds a PHP variable to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement. Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.

Most parameters are input parameters, that is, parameters that are used in a read-only fashion to build up the query. Some drivers support the invocation of stored procedures that return data as output parameters, and some also as input/output parameters that both send in data and are updated to receive it.

기본적으로 bindValue allow 직접 값 또는 상수를 바인딩하는 반면 bindParam은 변수 또는 참조를 전달해야합니다.

+0

이것은 정확히 내가 의심하는 바입니다. 그러나 이것은 매우 어리 석습니다 행동이지? 자동으로 NULL로 변환 할 수있는 방법이 있습니까? – wnstnsmth

+0

업데이트 된 답변 : bindValue와 bindParam의 차이점은 무엇입니까? – wnstnsmth

+0

@wnstnsmth : bindParam에는 변수가 필요하지만 bindValue에는 변수가 필요하지 않습니다. – TheCarver

관련 문제