1

I'm getting crazy trying to call, inside PHP code, a function that returns void, with pg_prepare/pg__execute...

$result = pg_prepare($dbconn, "",'SELECT * FROM dungeonasdb.crea_personaggio( "$nome", "$descrizione", "$email", "$password")');
$result = pg_execute($dbconn, "", NULL ) or die('Query creazione fallita');

Is there any settings of pg_prepare/pg_execute that I do wrong? Thanks

1
  • dungeonasdb.crea_personaggio is your table name ? Commented Jul 1, 2017 at 11:20

1 Answer 1

1

You probably want to do something different:

pg_prepare: don't put specific values, but placeholders. You will bind them to values later on. I prefer to always give statements a name.

If dungeonasdb.crea_personaggio is the function that returns void, you can call it by means of:

SELECT dungeonasdb.crea_personaggio(...)

You cannot call your function by means of SELECT * FROM dungeonasdb.crea_personaggio(...) because this would imply that dungeonasdb.crea_personaggio is a set returning function, which is not.

So you probably want your statements to be:

$result = pg_prepare($dbconn, "crea_personnagio", 
   'SELECT dungeonasdb.crea_personaggio($1, $2, $3, $4)'); 
$result = pg_execute($dbconn, "crea_personnagio", 
    ARRAY($nome, $descrizione, $email, $password)) 
    or die('Query execuzione fallita');

Your second statment is a pg_execute statement that binds parameters to the placedholders ($1, ...), and executes.

Sign up to request clarification or add additional context in comments.

4 Comments

ok I solved the problem, it was the function in the database, I wrote all the functions using only the tables name and not like 'dbname.tablename' . Also I think your answer it's not correct, beacause PERFORM works only inside postgresql functions , you can't use it from outside. But thanks for your time :)
You're right about PERFORM. I've corrected it. In PostgreSQL, what you're actually writing is schema_name.function_name(...) or schema_name.table_name (or, in general schema_name.object_name). You don't access two databases from the same connection (except through FDW). In PostgreSQL you have database -> schema -> object. In MySQL (for instance), you have only database (=schema) -> object.
Did the answer solve your problem? If so, it is customary to mark it as accepted, so that people can concentrate on the unanswered ones.
I haven't used your method but it works the same, now I accept it, thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.