Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling postgresql stored procedures from PHP

Status
Not open for further replies.

newbiepg

Programmer
Nov 6, 2002
181
IN
How can I call a postgresql stored procedure from PHP?
 
Same as any normal query:
Code:
$result = pg_query($dblink, "SELECT sp_name($arg1,$arg2,...);");

Now, whether you get the results you are expecting is another question. Remember, the procedure executes on the DB server-side, not in PHP. If the procedure returns results as a SET, then you should be able to loop through those results with pg_result() as if you had queried a table. If you are returning a single value as a result, you could just do something like

Code:
$value = pg_result(pg_query($dblink, "SELECT sp_name($arg1,$arg2,...);"),0,0);

Or, if you don't need a result, but just need to know if the procedure executed without error, then you could just do something like

Code:
pg_query($dblink, "SELECT sp_name($arg1,$arg2,...);")
   or die(pg_result_error);

Or

Code:
if(!pg_query($dblink, "SELECT sp_name($arg1,$arg2,...);"))
{
   //custom error handling
   my_error_handler(pg_result_error());
   //and, any other logic or output...
}
else
{
   //continue normal operations
}
-------------------------------------------

My PostgreSQL FAQ --
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top