Connecting to Heroku Postgres database with PHP - php

I've got an app with Heroku that I'm trying to connect to in PHP, but I'm not having any luck with. I'm able to connect to the database in pgAdmin III, so I know I've got the right info, but PHP isn't returning anything useful to me, only false. Can anyone suggest what I might be doing wrong, or even a way to get PHP to tell me something more substantial so I can troubleshoot better?
Here's my connection string:
$conn = pg_connect("host=ec2-xx-xx-xxx-xx.compute-1.amazonaws.com port=xxxx dbname=namestring user=username password=pass sslmode=require");
I've tried it with sslmode and without, I've tried changing the order of the parameters in the string, I just get false back when I do var_dump($conn).

I am assuming you are running this locally, so check that display_errors is enabled in your local php.ini and that error_reporting is set to something reasonable. That would be the reason you're not getting error output.
You should then see that either the connection is refused for some reason (could be your network), or that e.g. your local PHP doesn't have OpenSSL enabled (globally or for Postgres).
If this is happening on Heroku, then you'll never see errors in the page because it's a production env where display_errors should never be on; run heroku logs to see what PHP says. In this case the only reason would be that your credentials are incorrect, so check them for typos.
Which brings me to some important advice: do not hard-code these credentials (or any other config) in your code. Heroku gives you a DATABASE_URL env var with the connection string; read from that dynamically by using what parse_url(getenv('DATABASE_URL')) gives you. The reason is that your credentials may change in case Heroku needs to do an automatic failover of your database, and if you hard-code these credentials, your app would go down in this case.
If this is on Heroku, you really should run this stuff locally properly, that means with a local Postgres database as well, instead of connecting remotely to production databases, or even blindly pushing up your code to Heroku all the time without trying it locally. Much easier and faster over time.

Related

Can connect to database on server but cannot insert. Works on mamp localhost

I created a website which was fully functional on my computer's MAMP. When I placed it on the server, everything works well except for the fact that I cannot insert a new user into the DB. I'm partially concerned that the SSL cert is interrupting it, but I don't know enough about this to determine that. To troubleshoot this I did the following:
1. confirmed the connection works by returning row numbers and data that I manually placed into the database
2. confirmed openSSL is active/on
3. confirmed proper placement of my ssl certs through a separate site that confirms their presence
4. checked the error log on the server (which showed no errors) and then created a script to cause an error as proof of principle it works
5.confirmed the phpmyadmin user has full privledges
6. I have two user types and scripts that both don't work that worked on MAMP so I'm fairly confident its not poor coding
Further, all my php sql statements have built in mysqli_error($conn) which show no errors, and when I attempted a basic
"INSERT INTO users (name) VALUES ('john')"
scenario, it was unsuccessful.
My feeling that it is the ssl stems from the fact that my other (non-encrypted) sites don't have this problem. Is there something basic I'm missing?
Its not the SSL, its your configuration. See into it. If you can insert while doing it in the phpmyadmin with a raw sql, then check your config like the hostname user and db name. What server you are using?

PHP Socket PDO SQLSTATE[HY000] [2002] No such file or directory

Introduction
For the past couple of days I’ve been messing about with PHP web sockets, in an attempt to retrieve some information in real-time for the user about the various different people connected.
I’m on Ubuntu, running the latest version of XAMPP (located under /opt/lampp/), the web socket I run using PHP which I’ve installed using apt-get install php. This opens a socket with no problem whatsoever.
The problem I’m facing has to do with establishing a connection to the database from the web socket with a little help from PDO. Whenever I attempt to establish the connection to the database located on localhost, I get the following error message:
SQLSTATE[HY000] [2002] No such file or directory
Since PHP and my MySQL server isn’t aware of one another, considering MySQL is run through the instance of XAMPP and the server is run using the version of PHP (which is the same as the one as for XAMPP) I’ve installed using apt-get, one might think that this is the cause of problems. But shouldn’t the server be reachable as long as you have the correct username and password to the server configuration?
Research
I’ve been doing quite some research on the subject, and have found multiple potential solutions for the problem, but neither one appears to do the job. For example, on this question, it is suggested that you can solve this error by doing some witchery to your server software, as well as PHP—I’ve tried this with to apparent success.
On other questions, it’s instead suggested that you change this DSN PDO setup:
$dsn = "mysql:host=$host;port=$port;dbname=$name;charset=$charset";
To instead of using "localhost" for the host, you stick with 127.0.0.1—as a matter of fact, I can’t even connect unto my server if I were to do this. Doing this, my XAMPP gets stuck in some form of endless loop. A minute or so after changing from 127.0.0.1, back to localhost, it starts working again.
I can’t remember where, but I also remember seeing somebody saying it’s not at all doable (connecting to MySQL from a PHP socket), which I’d find extremely strange.
Some people also seem to suggest switching to Node.js for handling real-time connections, but I’d very much like to keep it all PHP if possible.
Furthermore
I’m probably just being a complete moron (I was ;-)), but if anybody can point me in the right direction it would be highly appreciated!
you need to specify the path of the socket
example:
$dsn = 'mysql:dbname=testdb;unix_socket=/path/to/socket';
Try to search the file mysql.sock inside the /opt/lamp

No connection to Oracle DB after Linux upgrade and OCI8-Installation

OpenSuse Leap 42.1 (latest upgrade)
Apache2, PHP 5.5 installed
OCI8 installed
ORACLE 11g
Last week, I got some update issues with KDE and did an upgrade of OpenSuse Leap 42.1. I then went over the whole procedure of installing oci8 (as described in http://php.net/manual/en/oci8.installation.php and making sure to use the correct PECL) and everything worked fine.
But when I restarted my system a few hours later, I was't able to connect via hhtp to any vhost due to failed connection to the database.
I get no error logs anywhere, just a white 404 page or the message "Error: Could not connect to database. Aborting." (depending on which page I'm calling). Pages with no connection to the database work perfect!
SQLDeveloper connects without any proble, using the same parameters as in the config-files for the vhosts.
Anyone any idea?
Generally when you get a
ORA-12514
it means that the listenr you are connecting to knows not the servicename/SID you're attempting to connect to. If you get that error it should indicate that the client got some sort of response from the listener which should exclude all sorts of network issues.
This is very much similar to ORA-12514 TNS:listener does not currently know of service requested in connect descriptor
only that the issue sprouted up after an update.
I would first look in the client side tnsnames.ora and the server side listener.ora to check that everything is ok. Also check that the database is registered on the listener. Possibly also check that the db is remotely reachable from a known good client to exclude any issues on the listener configuration.
problem(s) solved!
as there were two different of them provoking a problem salad.
first, the update somehow managed to change some parameters in my config files, first of all the database name.
then, apache update from 2.2 to 2.4 DID change some things in the vhosts config files and therefore the errors were never logged in the former x_error.log and the 404 message was shown.
thx a lot for your response, louigi600!

Check whether PHP incompatibility or MySqli incompatibility the issue?

I created a site, built it and tested it on my own server. Now uploading to the client's server and every page with PHP/MySqli doesn't work. When the php/MySqli script appears in the page it just ends there, cutting out the rest of the content, footer etc.
Here is the version of the site on my server that works
http://progressivewebsolutions.ca/expmed/researchers.php
and here is the one on the clients server that doesnt work...
http://expmed.mcgill.ca/researchers.php
the client server is running php 5.2.6
mysql 5.0.45
On the dbconnect file it is supposed to throw an error if it cannot connect and does not throw the error. Regardless of login info and password.
My question is what tools exist to check this issue and solve it? I taught myself to code and am constantly learning, just need a bit of guidance.
Thank You,
I would look first in the web server's error log. Many PHP errors that are not output to the HTML are sent to the error log.
Make sure you have PHP error reporting enabled. See also How to get useful error messages in PHP?
If you want more help, I suggest you edit your question above and show the code you use to connect to the MySQL instance.
Another strategy is to log into a shell on the PHP appserver host and try using the mysql command-line client to connect to the MySQL instance, using the same host, user, password that your PHP script uses.
$ mysql -h mysqlhost -u username -p
Enter password: <enter password here>
That will test if the hosts can talk to each other, and whether the login works (keep in mind MySQL connection privileges may be restricted by host, so one login that works from one appserver may not work from other appservers).
Start to perform a phpMyInfo in order to see if Mysqli is "on".
On the other way, under PHP, in such a case, the basic way of putting some "echo" along the code is, maybe, the best way.
After a look at the phpinfo(),it seems error reporting is off. So even if you have some errors, you cant' see them.
Just add:
error_reporting(E_ALL);
ini_set('display_errors','On');
at the beginning of your page and maybe you'll see the errors.
Good luck!
Peter

Establish a simple mongohq connection

Is there a way to establish a connection to a cloud mongoDB (such as mongoHQ), with no drivers or third party apps, from a localhost server?
In other words, I want to host my php files locally, and connect to a mongoDB on the cloud.
I've been doing simple websites using php and MySQL as a hobbie for a few years, and I'm pretty decent at it, but I haven't been able to figure out how to do this one simple thing with Mongo.
Whenever I run a file that does:
$m = new Mongo('mongodb://username:password#staff.mongohq.com:XXXXX/database');
from a php file, I get a HTTP 500 error.
Any help would be greatly appreciated. In advance I apologize if this isn't clear enough, this is my first post on SO. Please let me know if more info is needed.
Have you looked at your PHP/apache error log to see what the HTTP 500 error is ? Try adding to the top of your script:
ini_set('display_errors',1);
error_reporting(E_ALL);
Based on your code sample, I would check that you have the Mongo module installed as it will generally not be included by default. Use phpinfo() and look for the mongo section.
If you want to use MongoHQ without installing a Mongo driver, you could also try their REST API which apparently is still in beta phase.

Categories