I am new to postgreSQL and web development and lately I tried to connect my previously created database via PHP. But things didn't worked out, I get the password authentication failed for the user postgres. I've been looking everywhere but can't find anything to solve this issue.
The database is hosted on my iMac, port 5432 and php script on heliohost.org which will be used later on to query the database.
Here is my php script:
<?php
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=osm";
$credentials = "user=postgres password=newPassword";
$db = pg_connect( "$host $port $dbname $credentials" );
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
?>
My pg_hba.conf looks like this :
local all postgres md5
and my postgresql.conf has : listen_addresses = localhost
I am a bit confused with this, if anyone have suggestion to get this up and running.
Thanks everyone!
If you specify 127.0.0.1 as your host, it will attempt to hit your web server at heilohost.org, not your iMac. If you really want to do this (and there are a TON of reasons why it's a terrible idea), you'll have to set listen_address=0.0.0.0, forward the port on your router to your iMac, and use your public IP address in the connection string.
Related
I'm creating a PHP application and have a local test enviroment on both my laptop and desktop.
EDIT: Generated logs in a pastebin.
I am using the WPN-XM Serverstack.
Everything worked fine beforehand, but now on my laptop, when I try to do anything that requires a connection with the database, it throws this error.
No connection could be made because the target machine actively refused it
Did some research and found out that it could be a problem with my firewall, turned it off, same result.
Read elsewhere that this is not an issue with my code, this makes sense since everything works on my desktop.
Things I've tried so far:
Restarted the webserver
Restarted the laptop
Turned off the firewall
Login to phpMyAdmin (Error: Cannot log in to the MySQL server)
Changed the port of the webserver to 8080
Any idea what might be causing this error?
For completeness, altough the problem is more than likely not in the code, here' s the connection file.
$name = "root";
$pass = "";
$db = "myDB";
$host = "localhost";
$connect = mysqli_connect($host, $name, $pass, $db);
if (mysqli_connect_errno()) {
echo "Could not connect to the mysql database. Error: " . mysqli_connect_error();
}
Hey guys I have problem with connecting my .php file to database, I've tried different ways but still the same response.
Here is my code (updated):
<?php
$server_name = "localhost";
$mysql_username = "*****";
$mysql_password = "*****";
$db_name = "*****";
$conn = mysqli_connect ($server_name, $mysql_username, $mysql_password, $db_name);
if($conn) {
echo "Success";
}
else {
echo "Failed";
}
?>
UPDATE
Guys, when I connect to localhost everything works fine but I would like to connect to a server and be able to upload data from different android devices. Currently I use 000webhost.com free service, so might it be the key point and problem that I didn't upgrade that to PRO?
mysqli_connect requires the arguments to be in the order servername, username, password, databasename. Your order is servername, databasename, username, password.
The code should be:
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
You sequence of parameter is wrong
<?php
$server_name = "*****";
$db_name = "*****";
$mysql_username = "*****";
$mysql_password = "*****";
$conn = mysqli_connect($server_name, $mysql_username,$mysql_password, $db_name);
if($conn) {
echo "Success";
} else {
echo "Failed";
} ?>
Correct syntax
mysqli_connect(host,username,password,dbname,port,socket);
Reference mysqi_connect
EDIT
A extra tip
$conn = mysqli_connect("localhost","username","password","db_name");
Put values according to you server.
EDIT 2
MySQL needs to be running on the port 3306 make sure it is correct
From the comments and from the ruling out of other possibilities indicated in the other answers, it is quite possible it is a connection issue with your service provider.
You should write to them explaining your situation, if at all they have support for free services. You should probably change the passwords to some dummy ones in case you have share it with them; Some service providers ask you to share your password for them to check, not sure if it's recommended practice.
Also, for testing and development, it's better you use some kind of local installation of PHP, such as XAMPP or WAMPP etc, so that you can have control during development and testing. If something works on your local installation and not on the remote, having a development installation is useful to isolate and report such issues.
I think your problem is that you did not instantiate the class, remedied by making it $conn = new mysqli ($server_name, ..... . Also, your sequence of parameters is incorrect. It should be server, user, password and finally database.
In addition, you should be using PHP constants instead of variables for this, so $server_name should be SERVER_NAME. Similarly $mysqli_username, $mysqli_password, and $db_name should be changed. Not that I wouldn't work as you have it, but it just is not proper.
Iy you are using MAMP, the default port for mysql is 8889, but the "traditional" port that php expects to use for mysql is 3306.
So you need to open MAMP and change the MAMP mysql port to 3306, then restart the mysql server. Now the connection should be successful.
I try to connect my android application using JSON Parser to the web hosting. But whenever I try to connect (even just open using url in the browser) I will get the error message.
<?php
$dbHost = 'http://sql4.000webhost.com/localhost';
$dbUser = 'a6410240_cbetTD';
$dbPass = 'xxxxxx';
$dbName = 'a6410240_cbetTD';
$conn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($dbName,$conn);
?>
This is my database.php file. The full error message is
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'http' (4) in /home/a6410240/public_html/database.php on line 8.
I have tried change the $conn but still it didn't worked for me.
Thanks
If your database and application is on same server then use "locahost" in $dbhost.
And if your database and application is on different servers then you need to use IP address or hostname in $dbhost and the database user should be added on database server provided with required privileges.
The problem you are having was already mentioned in one of the comments, this one to be precise.
For your solution to work, all you need to do is omit the part http:// at the beginning and probably /localhost at the end.
The host is only the domain you are referring to. In this case sql4.000webhost.com. With /localhost you tried to already connect to a database, although your configured database is supposed to be a6410240_cbetTD.
MySQL use TCP port 3306 by default
($dbport) and hostname or IP address ($dbhost). For LAMP (Linux-Apache-MySQL-php) you can find a lot of tutorials.
Usually MySQL server listens internal port (which can't be reached via Internet) for security purposes.
If you familiar with docker, you can simply download examples of LAMP solutions from hub.docker.com.
there's this server:
http://phpmyadmin.pvdata.fr/index.php ,
I can connect to it knowing the username and password and Server Choice.
Once I connect to it, it shows under MySql the following information: (I'm Translating from French)
Server: sql6 (sql6 via TCP/IP)
Server Version: 5.0.92-87
Protocol Version: 10
Username: pvdata#10.5.1.3
Character for MySql: UTF-8 Unicode (utf8)
So I tried using the following code in order to connect to the Database:
<?php
// Create connection
$username = "pvdata";
$password = "xxxxxxxx";
$hostname = "10.5.1.3";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
?>
But it didn't work, it's saying: "A connection attempt failed because the connected party did not properly respond after a period of time".
What am I doing wrong?
Go to your phpMyAdmin installation and look for the file config.inc.php. Open that file, and check which hostname / ip is being used for SQL6. That is the IP you should use for your mysql_connect()
This is most likely a firewall issue. Most 3rd party hosts don't allow you to access MySQL from 'the outside'.
You can do this if you manage the MySQL server yourself or if you have a host that doesn't deny you to access the database from another machine, but in regular web hosting, this is not common.
You need to search, google helps a lot, also you need to connect to an ip/domain and port of your MySQL server.
For this, you need to know what is the port of your mysql server.
Look this answer from link
<?php
mysql_connect("mysite.com:3306", "admin", "1admin") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
to check if you have coneccion, try in console:
shell> mysql --host=remote.example.com --port=3306 //3306 is the
defaul port for MySQL
http://dev.mysql.com/doc/refman/5.0/en/connecting.html
I want to ask about how to connect my localhost application (C:xampp/htdocs/myproject) to database at my server host (www.someweb.somedomain)?
Am I possible to do that? If it is, how to connect it at my php config? Right now my config (server.php) is:
<?php
$host = "my web IP public:3306";
$user = "root";
$pass = "";
$db = "dispatcherDB";
$conn = mysql_connect($host, $user, $pass) or die ("Cant connect to mySQL");
mysql_select_db($db);
?>
what I got:
Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\XMS\server.php on line 7
So, what must I filled for $host? I'm trying using website IP, it still can't connect. Maybe there's someone here have experience at this problem?
Sorry for my bad English
If you have cPanel access to the remote server then you need to mention that from which ip addresses it should allow access to MySQL..
In cPanel you will get Remote MySQL under heading Databases:
Clicking Remote MySQL will give you the option to add hosts from where you want to allow connections to your MySQL server:
Also, you can check localhost without specifying port number as value of $host..
Probably Mysql server is not allowed root access from remote servers.
you could check this post