Subject | RE: [firebird-php] SPs Docs |
---|---|
Author | Nigel Weeks |
Post date | 2004-11-17T02:33:25Z |
OK. Here's an example
-----
First, create a table:
-----
isql into the database(or whatever tool you've got)
CREATE TABLE tbl_contact (
int_id NUMERIC(18,0) NOT NULL,
str_name VARCHAR(100) NOT NULL,
str_phone VARCHAR(10),
PRIMARY KEY(int_id),
);
CREATE GENERATOR GEN_TBL_CONTACT_ID;
-----
Now, we create a stored procedure to insert rows, and give back the ID
-----
SET TERM !! ;
CREATE PROCEDURE SP_INS_CONTACT (
str_name VARCHAR(100),
str_phone VARCHAR(10)
) RETURNS (
int_id NUMERIC(18,0)
)
AS BEGIN
/* Get the next value from our generator */
int_id = GEN_ID(GEN_TBL_CONTACT_ID,1);
/* Now insert the row */
INSERT INTO tbl_contact (int_id, str_name, str_phone)
VALUES (:int_id, :str_name, :str_phone);
/* And finally, suspend the new ID to the surface */
SUSPEND;
END !!
SET TERM ; !!
-----
Now, the PHP to run on top
-----
<?php
// Connect to the DB
$conn = ibase_connect("server:/path/to/database.fdb","sysdba","masterkey");
// Prepare an SQL string inside a variable
$inssql = "SELECT * FROM SP_INS_CONTACT('Henry Jones','12345678')";
// Run the query
$insrec = ibase_query($inssql);
// Get a result back(if any)
$insobj = ibase_fetch_object($insrec);
// Test the result
if($insobj->INT_ID != ""){
echo "Insert succeeded, and we have the id of ".$insobj->INT_ID."<br>";
echo "Lets see what we have in the database now:<br>";
$listsql = "SELECT * FROM tbl_contact order by str_name DESC";
$listrec = ibase_query($listsql);
while($listobj = ibase_fetch_object($listrec)){
echo "Name: ".$listobj->STR_NAME.", Phone: ".$obj->STR_PHONE,"<br>";
}
} else {
echo "It appears the insert failed";
}
?>
It's that easy! Have fun!
Nige.
-------------------------------------
<signature>
<name>
Nigel Weeks
</name>
<projects>
http://openaspect.sourceforge.net
</projects>
</signature>
-----
First, create a table:
-----
isql into the database(or whatever tool you've got)
CREATE TABLE tbl_contact (
int_id NUMERIC(18,0) NOT NULL,
str_name VARCHAR(100) NOT NULL,
str_phone VARCHAR(10),
PRIMARY KEY(int_id),
);
CREATE GENERATOR GEN_TBL_CONTACT_ID;
-----
Now, we create a stored procedure to insert rows, and give back the ID
-----
SET TERM !! ;
CREATE PROCEDURE SP_INS_CONTACT (
str_name VARCHAR(100),
str_phone VARCHAR(10)
) RETURNS (
int_id NUMERIC(18,0)
)
AS BEGIN
/* Get the next value from our generator */
int_id = GEN_ID(GEN_TBL_CONTACT_ID,1);
/* Now insert the row */
INSERT INTO tbl_contact (int_id, str_name, str_phone)
VALUES (:int_id, :str_name, :str_phone);
/* And finally, suspend the new ID to the surface */
SUSPEND;
END !!
SET TERM ; !!
-----
Now, the PHP to run on top
-----
<?php
// Connect to the DB
$conn = ibase_connect("server:/path/to/database.fdb","sysdba","masterkey");
// Prepare an SQL string inside a variable
$inssql = "SELECT * FROM SP_INS_CONTACT('Henry Jones','12345678')";
// Run the query
$insrec = ibase_query($inssql);
// Get a result back(if any)
$insobj = ibase_fetch_object($insrec);
// Test the result
if($insobj->INT_ID != ""){
echo "Insert succeeded, and we have the id of ".$insobj->INT_ID."<br>";
echo "Lets see what we have in the database now:<br>";
$listsql = "SELECT * FROM tbl_contact order by str_name DESC";
$listrec = ibase_query($listsql);
while($listobj = ibase_fetch_object($listrec)){
echo "Name: ".$listobj->STR_NAME.", Phone: ".$obj->STR_PHONE,"<br>";
}
} else {
echo "It appears the insert failed";
}
?>
It's that easy! Have fun!
Nige.
-------------------------------------
<signature>
<name>
Nigel Weeks
</name>
<projects>
http://openaspect.sourceforge.net
</projects>
</signature>