I am using this php to test if I am connected in a postgree database, works very well, but how can I insert a error Message and a Message showing the database is connected and not connected?
Example:
like: You are connect to:database_name
or:
You could not connect to:database_name
That is my code:
<?php
$connection = pg_connect ("host=localhost dbname=site user=postgres password=root");
?>
Just test the truthiness of the connection:
<?php
$connection = pg_connect ("host=localhost dbname=site user=postgres password=root");
if($connection) {
echo 'connected';
} else {
echo 'there has been an error connecting';
}
?>
Return value of pg_connect() is
PostgreSQL connection resource on success, FALSE on failure.
so check this value:
if (!$connection = pg_connect ("host=localhost dbname=site user=postgres password=root")) {
$error = error_get_last();
echo "Connection failed. Error was: ". $error['message']. "\n";
} else {
echo "Connection succesful.\n";
}
Related
I'm using this to connect to a PostgreSQL database with PHP 7.3. The connection fails, but the error message is empty. How can I see the error?
$sConn_string = "host=localhost port=5432 dbname=mydb user=postgres password=mypassword ";
$db = pg_connect($sConn_string);
if (!$db)
{
echo "Error 0: " . pg_last_error();
}
pg_result_error() also gives me an empty message.
I am trying to start a pgsql connection in php but i get pg_last_error(): No PostgreSQL link opened yet Please forgive my amateur question as i am a bit new to php.
Here is my php code:
<?php
$connect = pg_connect("host=xxx.xx.xxx.21 dbname=d106 user=b16 password=bran") or die("Could not connect: " . pg_last_error());
$result = pg_query($connect,"SELECT distinct thestartgeom FROM bikes");
if (!$result)
{
echo "no results ";
}
while($row = pg_fetch_array($result))
{
$coor = $row['thestartgeom'];
echo $coor;
}
pg_close($connect);
?>
In your pgsql connection you have missed the port number.
Try this way.
<?php
$host = "host=xxx.xx.xxx.21";
$port = "port=5432";
$dbname = "dbname=d106";
$credentials = "user=b16 password=bran";
$connect= pg_connect( "$host $port $dbname $credentials" ) or die("Could not connect: " . pg_last_error());
$result = pg_query($connect,"SELECT distinct thestartgeom FROM bikes");
if (!$result)
{
echo "no results ";
}
while($row = pg_fetch_array($result))
{
$coor = $row['thestartgeom'];
echo $coor;
}
pg_close($connect);
?>
You can store PgSQL connection code in one PHP file to reuse
pgsql_db_connection.php file
<?php
$host = "host=xxx.xx.xxx.21";
$port = "port=5432";
$dbname = "dbname=d106";
$credentials = "user=b16 password=bran";
$connect= pg_connect( "$host $port $dbname $credentials" );
if(!$connect){
echo "Error : Unable to open database\n";
}
?>
Call pgsql_db_connection.php file in other php files to use your database connection.
<?php
require_once('pgsql_db_connection.php');
$result = pg_query($connect,"SELECT distinct thestartgeom FROM bikes");
if (!$result)
{
echo pg_last_error($connect);
exit;
}
while($row = pg_fetch_array($result))
{
$coor = $row[0];
echo $coor;
}
?>
When pg_connect fails, it returns FALSE and produces a PHP warning with the detailed information on why it couldn't initiate the connection. If you can see the other message:pg_last_error(): No PostgreSQL link opened yet that you're reporting, I'd expect you should be able to see the previous one too, which is the one normally telling the reason of the failure.
If the display_errors configuration setting is set to 0, the first message would not show up on the browser/screen but the second would not either.
Anyway, assuming you can't have access to pg_connect warnings for whatever reason such as custom error handler, what's wrong with your code is that pg_last_error() must have an already opened connection to work.
To access the detailed error message from a failed pg_connect, the built-in PHP function error_get_last() (returning an array) could be used.
<?
$connect= pg_connect("your-connect-string");
if (!$connect) {
print_r(error_get_last());
// for only the message:
// echo error_get_last()['message']
}
die("DB connection failed");
?>
See also how to catch pg_connect() function error? if you prefer exceptions.
I'm using the following code to connect to a database that I have setup on godaddy.com. But when I try to run the script I get "Internal Server Error".
<?php
$db_conx = mysqli_connnect('MY_WEBSITE.secureserver.net', 'MY_USERNAME',
'MY_PASSWORD', 'DATABASE_NAME');
// Evaluate the connection
if (mysqli_connect_errno()) {
echo "cannot connect to database";
echo mysqli_connect_error();
exit();
}
else {
echo "Successful database connection, happy coding!!!";
}
?>
I'm not sure what i'm doing wrong
I'm new in this cpanel and I want to ask how to connect to Postgres in cpanel using php?
I use this simple code
<?php
$dbconn = pg_connect("host=localhost port=5432 dbname=test user=domain_test password=test")
or die('connection failed: ' . pg_last_error());
?>
and It keep on returning connection failed on my browser,
can somebody tell me how to do it correctly?
You can't really catch connection errors with pg_last_error. You will need to use pg_connection_status for that. But it will not give you enough info to take care of the connection issue.
It looks like error reporting is disabled in your case. So give this a try
error_reporting(E_ALL);
ini_set('display_errors', true);
$dbconn = pg_connect("host=localhost port=5432 dbname=test user=domain_test password=test");
$stat = pg_connection_status($dbconn);
if ($stat === PGSQL_CONNECTION_OK) {
echo 'Connection status ok';
} else {
echo 'Connection status bad';
}
I have a problem with my database! Here is my code:
<?php
$host = "/homes/49/jc192699/public_html/dbase";
$database = "EduPro.db";
$dbhandle = new PDO("sqlite:".$host.$database);
if (!$dbhandle){
echo "Error connecting to database.\n";
}
else{
echo "<br>";
echo "<br>";
echo "Database connection successful!";
}
mysql_select_db($database);
?>
The problem is that it's saying "Database connection successful!" No matter what I do, if I type the address in wrong, it still says successful, when I renamed the database to a database that doesn't exist, it still says successful. I can't see what the problem is here?
If anybody could help me out it would be GREATLY appreciated!
Thank you!
For starters, the PDO constructor throws an exception if there is an error. It does not return false. Check for errors using
try {
$dbhandle = new PDO("sqlite:".$host.$database);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Secondly, as you are using SQLite, provided your dbase directory is writeable by the script, your connection attempt will create an empty database.
Try this:
<?php
try {
/*** connect to SQLite database ***/
$dbh = new PDO("sqlite:/path/to/database.sdb");
}
catch(PDOException $e)
{
/*** real error message prints here ***/
echo $e->getMessage();
}
?>
This is directly taken from here:
http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#4.2