FireBird Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
파이어버드 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
IBPhoenix
FireBird Main site
볼랜드포럼 광고 모집

FireBird 팁&트릭
[10] php 에서 인터베이스 사용하기 - 예제 중심-
php [] 5194 읽음    2003-10-24 02:39
Creating a new database user
Before creating a new db, we create a user, which we will use for these examples (username 'phptest' and password 'phptest'). This can be done using the 'gsec' tool that comes with InterBase. Assuming your 'sysdba' password is 'masterkey', use it like this:


/opt/interbase/bin/gsec -user sysdba -password masterkey -add 'phptest' -pw 'phptest'


Creating a test database
Before creating the database, download this script: createdb.sql. (You might need to adapt the encoding for your country. ISO8859_1 fits well for western Europe.) The script looks like this:
SET SQL DIALECT 3;

CREATE DATABASE 'phptest.gdb'
PAGE_SIZE=8192
DEFAULT CHARACTER SET ISO8859_1;

CREATE TABLE ADDRESS
(
CATEGORY INTEGER NOT NULL,
NAME VARCHAR(100) NOT NULL,
KEYINDEX INTEGER NOT NULL,
ADDRESS BLOB SUB_TYPE TEXT SEGMENT SIZE 100,
PRIMARY KEY (KEYINDEX)
);

GRANT SELECT,DELETE,INSERT,UPDATE ON ADDRESS TO phptest;

commit;

Create the database, using this command:

/opt/interbase/bin/isql -i createdb.sql -u sysdba -p masterkey

It's always a good idea to keep scripts around that are able to (re)create your database(s). I remind you to change the 'sysdba' password, the whole world knows 'masterkey' ;-)



Some code examples
Note: These examples show you also how to integrate form-variables into your queries. They are derived from an application I wrote. (Look here for more.)

Select:
$dbh = ibase_connect('/home/yves/projects/php/phptest.gdb','phptest','phptest','ISO8859_1',0,1);
$sth = ibase_query('SELECT NAME,ADDRESS FROM ADDRESS WHERE KEYINDEX=?',$HTTP_POST_VARS['KEYINDEX']);
while ($row = ibase_fetch_row($sth)) {
print $row[0]."\n";
ibase_blob_echo($row[1]);
}
ibase_free_result($sth);
ibase_close($dbh);

The first line makes a connection to your database. Our sql statement is set in the second line. (Note how the form-variable KEYINDEX is substituted into the query.) Finally, with ibase_fetch_row($sth), we fetch line per line (well, only one in this example). In the loop, the db field NAME is printed by:

print $row[0]."\n";

As our field ADDRESS is a blob, is comes out with:

ibase_blob_echo($row[1]);

Text blobs need to be treated like this, you cannot print them with a simple 'print'.

InterBase blob functions are not documented in the php manual, but I found the functions in this document from Borland. It describes the php3 api, but all the functions I used happen to work in php4 as well.



Insert:
$dbh = ibase_connect('/home/yves/projects/php/phptest.gdb','phptest','phptest','ISO8859_1',0,1);

$blob_id = ibase_blob_create();
ibase_blob_add($blob_id,$HTTP_POST_VARS['ADDRESS']);
$blob_id_str = ibase_blob_close($blob_id);

$sth = ibase_prepare('INSERT INTO ADDRESS (KEYINDEX,NAME,ADDRESS,CATEGORY) VALUES (?,?,?,?)');
$trans=ibase_trans();
ibase_execute($sth,$HTTP_POST_VARS['KEYINDEX'],
  stripslashes(strip_tags($HTTP_POST_VARS['NAME'])),
  $blob_id_str,$HTTP_POST_VARS['CATEGORY']);
ibase_commit($trans);
ibase_free_query($sth);
ibase_close($dbh);

Take a look at the lines 3-5. The function ibase_blob_add is needed to prepare the data before you can insert it into the db. Actually, the data from the variable $HTTP_POST_VARS['ADDRESS'] is prepared and put into the blob, using $blob_id_str.



Update:
$dbh = ibase_connect('/home/yves/projects/php/phptest.gdb','phptest','phptest','ISO8859_1',0,1);
$sth = ibase_prepare('UPDATE ADDRESS SET NAME=? WHERE KEYINDEX=?');
$trans=ibase_trans();
ibase_execute($sth,stripslashes(strip_tags($HTTP_POST_VARS['NAME'])),$HTTP_POST_VARS['KEYINDEX']);
ibase_commit($trans);
ibase_free_query($sth);
ibase_close($dbh);

Not much to say for this example.


Well, that's it!
I hope this gives you a successful start with php and InterBase. It is always a good idea to look for existing projects that use php/InterBase. Searching freshmeat for 'php interbase' currently shows about a dozen.

+ -

관련 글 리스트
10 php 에서 인터베이스 사용하기 - 예제 중심- php 5194 2003/10/24
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.