Friday, July 10, 2009

How to call stored procedure in MDB2 php abstraction package

How to call stored procedure in MDB2 php abstraction package

you may be facing difficult in calling stored procedure in your MDB2 database package

here is a solution to over come the difficulty

in this process first create the db connection in your file and get the object of that connection the following code shows how to connect your db

require_once 'MDB2.php';

$con = MDB2::connect('pgsql://usr:pw@localhost/dbnam');
if (PEAR::isError($mdb2)) {
die($mdb2->getMessage());
}

assuming you have your db connected object in $con

and create function where you can execute your query string separately like shown below “doquery” function

function doquery()

{

if(empty($con))

$con = $dao->Connect();

$resultset = $con->query($sql);

if(PEAR::isError($resultset)) {

die("Dao::doQuery() ::: Error while executing query: " . $sql . "
Error Message:" . $resultset->getMessage());

}

$object = Array();

while($row = $resultset->fetchRow()) {

$object[] = $row;

}

return $object;

}

Then in next step you can have call to the stored procedure in your php file where you want to call your stored procedure

The below code shows you how to call

$query = "CALL storedproceduer_name($parameter1,$parameter2,….);";

$result_value = Dao::doQuery($query);

This $result_value will gives you the result set of stored procedure

And you can test this by

Print_r($result_value);

No comments: