pdo로 prodcedure 사용시 잘못된 결과값을 보여줄때.

pdo를 사용해보려고, 서버 셋팅 및 기본 코딩을 잡아 보고 있던중에
스토어드프로시져(내부에 동적쿼리로 작성)를 사용중에 결과값이 잘못가져오는 경우가 발생.
결과값이 컬럼과 value가 제대로 매칭되지 않는 문제가 발생했다.
웹 서버 버젼은 
- php  version 5.1.6
- mysql client API version 5.0.37 
이다.
이 환경에서 동적쿼리가 포함된 프로시져 사용시 결과값이 잘못 가져오는 경우가 생겨서 한참 고생했다. 구글링을 한참 하다 보니, PDO::ATTR_EMULATE_PREPARES 이부분을 프로그램에 적용후 해당 문제가 해결되었다.
정확한 이유는 모르겠으나, 대략적으로 쿼리 파싱을 PDO쪽에서 할건지 mysql서버쪽에서 할건지를 결정하는거 같다. 이부분을 동적쿼리인경우 PDO쪽에서 하도록 해줘서 해결된 문제다.(prepared를 client 또는 server쪽에서 하도록 정하는 부분인거 같은데, 서버쪽에서 하는것이 10%정도 더 효과가 있다고 한다.)
PDO에 대한 버젼에 따라서 이런 문제가 있지 않나 싶다.

프로그램 부분은

$dbh = new PDO('mysql:host=db서버;dbname=데이타베이스', '계정','비밀번호',array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES euckr"));
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);

이렇게 해주면된다.
이부분이 없이 PDO사용이 가능하다면 빼주는것이 좋다고 한다. 

이걸 해결해 볼려고, 구글링을 하다가 찾은 여러 포스트중 아래 두개 포스트에서 많은 이해를 할수 있었다.(영문이라, 나름 이해하는라 힘들었음..ㅠㅠ)
http://wezfurlong.org/blog/2006/apr/using-pdo-mysql/
http://isp-control.net/ispcp/ticket/2402
인용 : 

The "ATTR_EMULATE_PREPARES" attribute allows to emulate the prepared queries for RDBMS that don't support this feature. By default, emulation is actives (ATTR_EMULATE_PREPARES == TRUE). That means that the queries will not be prepared by the Mysql Server (Server-Side) but by PDO (Client-Side). The final result is identical, all the parameters are automatically quoted and so, we have not problem with SQL injection in both cases.

Now, what is the subject here ? What the difference with the native and emulated support for prepared queries ?

Some RDBMS don't support prepared queries. That means that the queries will be prepared by PDO (

Client-Side

) and not by the Server (

Server-side

). The final result is the same but for performances reasons, it's better to use native support when it's possible. Indeed, for example, with Mysql, we can take 10 % free.

Th remaining problem with Mysql is that for versions prior 5.1.17 (in reality 5.1.21 for us), the prepared queries can't be cached. So, we have 10 % free but we lost 20 - 35 %...

That for why, the native support should be used when possible (only with Mysql server >= 5.1.21 (for us).

To resume, the parameter ATTR_EMULATE_PREPARES should be set to TRUE for Mysql server version prior 5.1.21 and to FALSE for other versions. By default the ATTR_EMULATE_PREPARESis set to TRUE.

TRUE: Support for prepared queries is emulated. FALSE: Support for prepared queries is not emulated. The native support is used.
 

나중에 서버 php버젼을 업그래이드 하게되면, 다시한번 살펴봐야겠다.


 

PDO 소개 및 예제

PDO에 대해서 소개해주는 곳이다.

http://docs.cena.co.kr/?mid=textyle&document_srl=11910
http://kr.php.net/manual/kr/book.pdo.php
http://www.phpwelt.net/handbuecher/korean/ref.pdo.html

--PDO설치하기.
http://www.barney.pe.kr/blog/1181
http://ezgreg.tistory.com/46