sansetty25
sansetty25

Reputation: 11

Call PostgreSQL function in PHP

CREATE OR REPLACE FUNCTION adduser(fn character varying,ln character varying,cit character varying,i integer,id integer)
  RETURNS void AS
$BODY$
        BEGIN                
    update tabtow set fname=fn,lname=ln,city=cit,phnum=i where phnum=id;
    select * from tabtow;
        END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION increment(integer)
  OWNER TO postgres;

This is my function, how can I call it in PHP?

I'm trying like this, but it isn't working:

$sql = "update tabtow set fname = '$_REQUEST[fn]', lname = '$_REQUEST[ln]', city = '$_REQUEST[ci]', phnum = $_REQUEST[phnum] where phnum = $_REQUEST[id1]";
$res = pg_query($sql);

Upvotes: 1

Views: 6173

Answers (1)

Richard Huxton
Richard Huxton

Reputation: 22893

Just call the function:

$sql = 'SELECT adduser($1,$2,$3,$4)';
$res = pg_prepare($dbconn, "my_query", $sql);
$res = pg_execute($dbconn, "my_query", array('a','b','c',1));

And like the commenters above say, don't build your queries by hand - use parameterised queries like above.

Upvotes: 3

Related Questions