0

I'm currently trying to retrieve information from my database and display this data with a php file but it doesnt work as intended. Here is my code:

<?php
  require("dbconnect.php");

  if(!$db){
   echo "Error: Unable to open database";
  } else {
   echo "Opened database successfully";
  }

  $result = pq_query($db, 'SELECT * FROM example');
  if (!$result) {
    echo "Error: Cant access table";
    exit;
  }
  else {
    echo "Everything works fine";
  }

  pg_close($db);
?>

Note: dbconnect.php opens the connection to the database with pg_connect() and saves this to $db.

I expected that it displays Opened database successfully Error: Cant access table because I havent created a table example yet. But I only get Opened database successfully. So I added echo "Test"; before pg_close($db); and expected that it displays Opened database successfully Test but no, it only shows Opened database successfully.

I then tried to create a new table with php so I added

pg_query($db, 'DROP TABLE IF EXISTS example');
pg_query($db, 'CREATE TABLE example (col char(1))');

before $result = pq_query($db, 'SELECT * FROM example');. I connected with ssh to the server after this and checked with psql if the table exists and it did, so the connection should work properly. But it still only shows me Opened database successfully and I expected Opened database successfully Everything works fine. I really dont know why every echo after $result = pq_query($db, 'SELECT * FROM example'); doesnt work. Can someone explain to me whats wrong?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
JuarX
  • 21
  • 5

1 Answers1

0

Change pq_query for pg_query.

Note you have a Q instead of a G.

Pablo Pazos
  • 3,080
  • 29
  • 42