0

I was wondering if it's possible to prepare statements using PHP's pgsql library that do not require parameters.

I'm really used to preparing all my SQL statements in the beginning of a program instead of when I need them so my code always looks similar to this

<?php

    $pgsqlCon = // code to connect


    // prepare SQL statements

    $sql = "SELECT * FROM table01 " .
           "WHERE t01_idno >= $1";
    pg_prepare($pgsqlCon, "table01_cur", $sql);

    $sql = "SELECT * FROM table02 " .
           "WHERE t02_idno = $1";
    pg_prepare($pgsqlCon, "table02_sel", $sql);


    // start main

    $table01 = pg_execute($pgsqlCon, "table01_cur", array('1'));
    while ($row = pg_fetch_row($table01)) {
        echo "<span>found something<br /></span>";
    }

    $table02 = pg_execute($pgsqlCon, "table02_sel", array('1'));
    while ($row = pg_fetch_row($table02)) {
        echo "<span>found something else<br /></span>";
    }


?>

So I would like to prepare statements that don't require parameters in this way as well if that is possible.

2
  • 1
    I'm not familiar with the pg_* functions but I would start with trying it using an empty array as the third parameter. Or have you done that already? Commented Nov 3, 2015 at 12:42
  • 1
    Ah passing the empty array worked, I tried to leave off the array completely which was the thing breaking it, thanks a bunch. Would you like to post that as an answer so that I can accept it as the right one? Commented Nov 3, 2015 at 12:51

1 Answer 1

2

I had the same problem and sadly this usage is not documented in the official documentation.

Looking at it you can see that the third argument is not optional (i.e. not in brackets [..]) and indeed, as the above comment says, passing an empty array() as the third argument works.

So after preparing a statement with no arguments one could execute it like so:

$rs = pg_execute($connection, $stmt, array());

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

1 Comment

yeah i mean im not sure if there are any performance benefits of using prepared statements in php pgsql but it still would be nice to have documentation about some less conventional uses

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.