0

I've been trying to connect to my database, which has been created with PostgreSQL. However, it's unsuccessful.

I've watched lots of videos and researched quite a bit on here, but my issue seems too simple to be an issue for others.


The setup:

I'm hosting a local MAMP (free version) server, in order to use PHP. I've downloaded and created a database using PostgreSQL locally, and am doing it from pgAdmin 4.

The code is written in a .php file, surrounded by PHP clamps, and the first print line is visible, so I believe there is a connection to the web page in my chrome browser.

I don't get any response from my if-else statement or the pg_query. All the page shows is: This is it:

Issue in short: What is the cause of me not being able to connect to the database and get data from it?

print "This is it:"; 

$db_connection = pg_connect("host=localhost dbname=first_creation user=postgres password=1234");

if($db_connection) 
{
    echo "connected";
}
else {
    echo "not working";
}
$result = pg_query($db_connection, "SELECT text_content FROM strings");  
4
  • Do you have your server configured to support pgsql? Anything in the server logs that might point to the issue? Another option is to use the PDO extension to connect. Commented Feb 4, 2022 at 18:12
  • I would try connecting from the command line first, via psql. You should at least get some useful errors that may help. Commented Feb 4, 2022 at 18:37
  • I have not changed the configuration of MAMP if that is what you mean. "PhpPgAdmin" is installed with MAMP, and it seems that it is this software that can do the trick. If any further configuration/enabling is needed, I don't know how to. Edit: I tried the PDO extension, but it doesn't seem to do the trick. Commented Feb 4, 2022 at 18:43
  • @Alan I can access my database via CMD and psql without errors at all. Commented Feb 4, 2022 at 18:52

1 Answer 1

0

you need to instll and configure the driver of pgsql in php.ini, please check the following question: How do I enable php to work with postgresql?

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

Comments

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.