Background Image
조회 수 31628 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
php에서 객체를 문자열로 변환해서 DB에 저장할 때가 있다. 이때 주의할 점..
반환 값이 

Returns a string containing a byte-stream representation of value that can be stored anywhere.
바이트 스트림이기 때문에 일반적인 문자열로 생각할 경우 곤란할 경우가 생긴다.
.

If you are serializing an object with private variables, beware. The serialize() function returns a string with null (x00) characters embedded within it, which you have to escape. Not great if you're trying to save objects into a DB...

위의 글과 마찬가지로 private 변수를 사용할 경우
null(x00) 값이 들어가기 때문에 자칫 c나 php 함수를 사용해서 db에 저장하는 경우 중간에 끊기는 경우가 발생할 수 있다.
(가장 좋은 방법은 blob 같은 데이터 타입을 사용하는 것이다.)

테이블의 컬럼은 VARCHAR 형태로 만든 후
base64_encode()를 사용해서 변경하면 객체를 serialize한 값을 사용할 수 있다.

ex)
 -- 테이블 생성
CREATE TABLE "test_bind"(
    "id" integer AUTO_INCREMENT,
    "var" character varying(1073741823)
);



php 예제
 <?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

$server = "127.0.0.1";
$port = 33000;
$dbName = 'testdb';
$user = 'dba';
$password = 'cubrid';

/*
CREATE TABLE "test_bind"(
    "id" integer AUTO_INCREMENT,
    "var" character varying(1073741823)
);
*/

class ParserOutput
{
    var $mText = 'test';
    private $mIndexPolicy = '';
    private $displayTitle = false;
}


$po = new ParserOutput();
$value = serialize ( $po );
$value = base64_encode( $value );

$con = cubrid_connect($server, $port, $dbName, $user, $password);
if ($con) {
   echo "connected successfully<br/>";
    
   
   $sql = "insert into test_bind(var) values ( ? )";
   $req = cubrid_prepare( $con, $sql );
   
   print "cubrid_bind()";
   $res = cubrid_bind( $req, 1, $value); print " -- OK<br/>";
   
   $res = cubrid_execute( $req );
   print "result: $res <br/>";
   
   if (cubrid_error_code() > 0) {
    print "ERRORCODE:" . cubrid_error_code() . "<br/>";
    print "ERROR:" . cubrid_error_msg() . "<br/>";
   }

   cubrid_commit($con);
   cubrid_disconnect ($con);
}




TAG •

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
179 기타 utf-8 데이터 like 검색 시 설정 손승일 2009.05.12 18453
178 응용개발 tomcat 5.5 버전 이상에서 dbcp(커넥션 풀링) 설정법 admin 2008.11.21 28527
177 질의작성 synonym에 대하여 김민종 2023.09.07 257
176 질의작성 subquery를 이용한 튜닝예제 file 김창휘 2016.12.27 3322
175 질의작성 select 결과중 몇개만 가져오는 방법 admin 2008.11.21 19917
174 질의작성 pivot() 간략하게 구현하기 남재우 2009.06.24 23426
» 응용개발 php에서 serialize/unserialize 사용 시 주의할 점 시난 2009.12.29 31628
172 응용개발 maven에서 CUBRID JDBC Driver 추가하기 file 김승훈 2015.12.09 8944
171 질의작성 loose index scan을 활용한 효과적인 쿼리 튜닝 방안 박세훈 2017.06.01 2409
170 응용개발 loadjava 사용 시 inner class 로딩은 어떻게? admin 2008.11.21 27450
169 운영관리 linux에서 ipcs를 이용한 CUBRID의 Shared Memory 확인하는 방법 seongjoon 2010.03.31 25821
168 운영관리 libjvm.so 찾지 못하여 서버 구동이 되지 않은 경우 admin 2008.11.21 26465
167 질의작성 join update 처리방법입니다.(연관성 있는 테이블을 조인하여 처리하는 UPDATE 구문) 권호일 2012.11.30 20394
166 응용개발 jdbc에서 질의 플랜정보 보는 방법 손승일 2009.04.15 15840
165 응용개발 jdbc에서 bit 데이터 타입 사용하기 손승일 2009.04.11 15013
164 CUBRID 매니저 java.lang.OutOfMemoryError 처리 방법 file janus 2009.04.13 17880
163 운영관리 insert 질의로 16K 이상 데이터 입력 시 오류 문제와 해결방법 정만영 2010.07.01 13184
162 질의작성 group_concat(문자열 그룹처리) 함수 사용하기 권호일 2016.06.29 28246
161 질의작성 group by 사용 방법 및 여러 필드중 하나의 필드에 대하여만 group by 처리 방법 남재우 2010.02.26 21711
160 기타 glo 사용시 loaddb 주의 사항 웁쓰 2009.12.31 18650
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 14 Next
/ 14

Contact Cubrid

대표전화 070-4077-2110 / 기술문의 070-4077-2113 / 영업문의 070-4077-2112 / Email. contact_at_cubrid.com
Contact Sales