Search code examples
phppostgresqlprepared-statement

pg_prepare(): Query failed: ERROR: Relation does not exist


I am trying to execute a query on a Postgres database using PHP. I get the following error:

pg_prepare(): Query failed: ERROR: relation "login" does not exist LINE 1: SELECT * FROM login 

This is a snippet of my code:

$connString = "host= port= dbname= user=password=";
$conn = pg_connect($connString);
if ($conn) {
    $sql = 'SELECT * FROM login ';
    if (pg_prepare($conn, "myQuery", $sql)) {
        $result = pg_execute($conn, "myQuery", array());
        if ($result) {
            while ($row = pg_fetch_row($result)) {

                echo $row[0];

                echo $row[1];
            }
        } else {
            echo 'executing error!';
        }
    } else {
        echo 'preparing error!';
    }
} else {
    echo "Connection error!";
}
if ($conn) {
    pg_close($conn);
}

When i run this script, it also prints 'preparing error!'

I guess there is something going on within the database. I have tried to create a new table. I also tried to use quotes in the query on several places, but nothing works.

This is the query I used to create the database:

CREATE TABLE login
(
    user_id integer NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    username character varying(50) COLLATE pg_catalog."default",
    pwd character varying(50) COLLATE pg_catalog."default",
    CONSTRAINT pk_user_id PRIMARY KEY (user_id)
)

How do I fix this?


Solution

  • Try

    SELECT * FROM public."login"