PHPBuilder - Getting started with php and InterBase Page 6



RSS Twitter
Articles Databases

Getting started with php and InterBase - Page 6

by: Yves Glodt
|
February 12, 2002

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.
Have fun! Other questions? Post them here.
-- Yves Glodt

« Previous Page
1
|
2
|
3
|
4
|
5
|
6

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Yves Glodt

Comment:



Comment:

(Maximum characters: 1200). You have characters left.