cannot connect php to postgres database using pg_connect - php

I just try to connect my php code to postgres database but unfortunately it is not work, here is my code :
<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=public";
$credentials = "user=postgres password=postgres";
$db = pg_connect( "$host $port $dbname $credentials" ) or die('Could not connect');;
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
browser result :
Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: database "public" does not exist in /var/www/test/pgtest.php on line 11
anybody know what is wrong here ?

Are you sure the name of database is 'public'? As fas as I remember 'public' is a name of default schema inside the PostgreSQL database.
Please make sure the database 'public' really exists.

From the command line, become the postgres user, start psql, and issue the \list command to list your databases. Be sure the database you're trying to open is in the list.
Be sure your pg_hba.conf file allows the database to be opened.
Remove everything except $dbname from the pg_connect call and allow the API to use the defaults. Add parameters back as indicated by the error message(s).

sorry but finally I found that my question was wrong, public and testdb are Schemes inside postgres database,

Related

How to connect to mysql database with php?

I'm using Jetbrains and Mysql to work on this practical project, but when I connect to the mysql
database it gives me the following error:
C:\wamp64\bin\php\php5.6.40\php.exe C:\wamp64\www\Social_Network\Includes\connection.php
Connection failed: SQLSTATE[HY000] [1049] Unknown database 'social_network'
Process finished with exit code 0
I made sure several times that the database name is the same name and there are
no spelling errors at all (I copy pasted it from the database)
Here's my code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=social_network", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
//?>
Welll, there's little that can be done about it. MySQL thinks that the database does not exist.
is the server the correct one?
is the case sensitivity set correctly? "Social_Network" and "social_network" might be considered different.
can you access the database with those parameters using a different tool (e.g. HeidiSQL, SQLYog, SQLterm, in a pinch even phpMyAdmin)?
Actually, JetBrains PHPStorm has a SQL terminal utility that can diagnose the connection. You may want to use it (once it knows what database you're connecting to, it will also warn you of several possible errors such as using the wrong table name or column name).

Database is created but return error as unknown database

I already have created my database, and already have the pages in PHP ready to be connected to the database. My code of connection is the follow :
<?php
$host = "localhost";
$db = "clients";
$user = "root";
$pass = "";
$con = mysqli_connect($host,$user,$pass,$db) or die('Could not connect to MySQL: ' .mysqli_connect_error());
?>
When I try to execute the page with WAMP, it returns me the message :
Warning: mysqli_connect(): (HY000/1049): Unknown database 'clients'
Even the database being already created and the queries executed, it still gives me this error, as if the database was with the wrong name in the PHP or something.
Maybe is the error from not having some password from the user root or using localhost ? How can I fix this ?
I came across this error sometime ago while using MySQL database with node.js, I just deleted the database and created it again. It's funny though, but that was how I solved the error. Try creating the database again with another database name.

Cloud9 and MySQL, Connection Refused

I'm sure this will be pretty easy, but I've been looking at this for a while, and haven't figured it out yet.
I've got a project in Cloud9 that I am trying to set up to access a MySQL database.
I've started the MySQL engine (mysql-ctl start), so I don't think that's the issue.
Here are my variables:
$db_hostname = "[username]-[projectname]-[assigned id]"; //this was retrieved by using 'SELECT ##hostname;' within MySQL
$db_database = "c9"; //default cloud9 database
$db_username = "[username]";
$db_password = ""; //the default is no password
$db_port = 3306; //the default cloud9 MySQL port
And here is my mysqli connect statement:
$conn = mysqli_connect($db_hostname, $db_username, $db_password, $db_database, $db_port) or die("Connection failed: " . mysqli_connect_error());
And this is the error that is being sent back:
Warning: mysqli_connect(): (HY000/2002): Connection refused in
/home/ubuntu/workspace/insertWorkoutScript.php on line 11
Call Stack:
0.0009 248728 1. {main}() /home/ubuntu/workspace/insertWorkoutScript.php:0
0.0012 249768 2.
mysqli_connect() /home/ubuntu/workspace/insertWorkoutScript.php:11
Connection failed: Connection refused
Line 11 (the line that the error identifies) is the $conn = mysqli ... statement. I've verified that the variables are being filled (I've got an echo for each variable name and it's value). I've double and triple checked the value I'm using for $db_hostname (which as I said, I retrieved from cloud9 using the SELECT ##hostname; statement). And as I said, I've made sure to Start the MySQL instance with the $ mysql-ctl start line in the terminal. Thoughts on what simple thing I'm missing here?
Thanks
Thank you #HPierce for the reminder to try getenv('IP'). I had tried that earlier but got a bad function response. My guess is that I typed it incorrectly, or was using it incorrectly. I've tried that again, and am now getting a good connection. So the correct settings are:
$db_hostname = getenv('IP'); // as opposed to using results from select ##hostname;
$db_database = "c9"; //default cloud9 database
$db_username = "[username]";
$db_password = ""; //the default is no password
$db_port = 3306; //the default cloud9 MySQL port
Everything else stayed the same. And I've also now solved my SQL insertion error as well.
Thanks again for your help.

PHP - Cannot connect to MySQL database

I am trying to connect to my MySQL database through php, I am managing my Database with phpmyadmin. To login the username is root and I dont have a password. My problem is I cant connect, I get the "Could not connect to mySQL database" message when I try to
Below is my Code
<?php
session_start();
$server = 'localhost';
$db_usernmae = 'root';
$db_password = '';
$database = 'househockey';
if(mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
if (mysql_select_db($database)) {
# code...
die('couldnt connect to database');
}
?>
Im not sure if it matters, but I am using WAMP and I put my phpmyadmin folder into my htdocs folder.
Thanks
As you have written your code :
if(mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
This will when connection is true print the following: die('Could not connect to mySQL database'); I think what you need to test your connection, which sounds like it should work:
if(!mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
The ! will negate the returned value of your mysql_connect and tell you if you're connected.
As far as I know, PMA explicitly needs a username and password. Set a root password through
mysqladmin -u root password NEWPASSWORD and then change your PMA config, followed by a server restart. Alternatively, use MySQL workbench. It does more than create entity relationship diagrams (ERDs).
You may run into this problem if you have an anonymous user defined on your database.
"When a client tries to connect to the database, the MySQL server looks through the rows in the user table in a sorted order.
The server uses the first row that matches the most specific username and hostname."
Delete the anonymous user and try again.

unable to connect to mySQL Db

I am a newbie to PHP and to the net world, and trying to connect to a mySQL Db using this PHP code:
<?php
echo "Hello World \n";
$mysql_host = "MySQL Host"; // this is what specified to use in mySQL management page at my host
$mysql_database = "mysql_database";
$mysql_user = "mysql_user";
$mysql_password = "mysql_password";
echo $mysql_host;
$conn = mysql_connect($mysql_host, $mysql_user, $mysql_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db($mysql_database);
if (!$db_selected) {
die ('Can\'t use '.$mysql_database.' : ' . mysql_error());
}
?>
please, what can be wrong with this code? I couldn't find any syntax error and still - I am not able to connect to the Db
can it be and the problem is related to the host only?
The following variables should contain values that are respectively:
$mysql_host - IP or host name of the server with your database,
$mysql_database - name of the database,
$mysql_user - name of the database user,
$mysql_password - password for specific user to the specific database,
Make sure all of them are correct and then the problem should be resolved. If it is not, let us know.
If you are actually typing MySQL Host in your code, then you're not providing a valid hostname. You should try with localhost instead.
Otherwise, you current host must have provided you with the needed information.
You should also tell us what error your script gives you, when it dies. That enables us to give a qualified answer instead of guessing.
as #Michael says,
$mysql_host = "MySQL Host";
is definitely wrong, that field must have a either a valid host/socketname or localhost..
These guys are spot on. Also, the password is usually blank on a localhost program.
But, this is just something I noticed:
Your $conn variable is missing an 'n' in your 'if' statement.
You are storing connection in $conn but in if condition you are checking with ($con).
(There might be other errors. But I can see this error)

Categories