Background Image
조회 수 31631 추천 수 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
번호 분류 제목 글쓴이 날짜 조회 수
16 응용개발 CUBRID에서 제약조건(PK,FK,UNIQUE),index 설정 시 주의 사항 cubebridge 2009.11.26 18886
15 응용개발 CUBRID에서 systimestamp, sysdatetime default값에 대한 정의 cubebridge 2009.12.16 28269
14 응용개발 CUBRID에서 euc-kr의 한글 데이터를 utf-8의 한글 데이터로 변경 시 주의할 점 cubebridge 2010.04.01 22407
13 응용개발 CUBRID에서 SQL힌트와 주석을 쓸 때 순서. cubebridge 2012.01.11 13329
12 응용개발 CUBRID에서 CHAR와 NCHAR의 차이점 cubebridge 2010.07.01 21320
11 응용개발 CUBRID2008 Java Stored Procedure 등록 방법 cubebridge 2010.02.24 19564
10 응용개발 CUBRID-PHP-Driver 연동가이드 file cubebridge 2012.11.13 17136
9 응용개발 CUBRID 와 DBCP의 관계 엄기호 2016.12.14 2805
8 응용개발 CUBRID 에서 제공하는 ODBC 함수 목록 admin 2008.11.21 26052
7 응용개발 CUBRID odbc 드라이버 설정방법. file seongjoon 2009.12.10 20328
6 응용개발 CUBRID R3.0부터 JDBC를 사용하는 응용에서 charset 설정 방법이 변경되었네요. 손승일 2010.12.02 19635
5 응용개발 CUBRID PHP 드라이버 빌드와 연동방법 정만영 2018.03.07 3773
4 응용개발 CUBRID HA 제약 사항 및 확인 방법 손승일 2015.12.29 5305
3 응용개발 CUBRID DB에서 critical section 정보 출력하기 file 주현 2015.12.31 4613
2 응용개발 CUBRID AUTO_INCREMENT 컬럼 MySQL LAST_INSERT_ID() 대체 방법 손승일 2009.12.22 33235
1 응용개발 Attempt to access a closed ResultSet(PreparedStatement) 오류 admin 2008.11.21 28036
Board Pagination Prev 1 2 3 Next
/ 3

Contact Cubrid

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