/    Sign up×
Community /Pin to ProfileBookmark

DB Error: connect failed…php and Postgresql

hi…
I am working on PHP using postgreSQl as DB…Linux as OS
Both r very new to me….
i logged in as root and
created a user root
the command i hav used : [B]createuser root[/B]

and
database sample : [B]createdb sample[/B]
then to check list of database i used command : psql -l
the RESULT was

[QUOTE]

List of databases
Name | Owner | Encoding
———–+———-+———-
sample | root | UNICODE
template0 | postgres | UNICODE
template1 | postgres | UNICODE
(3 rows)

[/QUOTE]

then i created a table in DB sample name “login”
using normal sql query and inserted two datas
i created file “show.php” to display the content on web page

the code is as follows :-

[QUOTE]

<?php
include(“DB.php”);

$username = “root”;
$password = “”;
$hostname = “localhost”;
$dbname = “sample”;

$dsn = “pgsql://$username:$password@$hostname/$dbname”;
echo “The DSN : $dsn<br>”;

$con = DB::connect($dsn);
echo “The Connection Object : $con<br>”;

if(DB::isError($con))
{
$valu = DB::isError($con);
echo “Error Number : $valu <br>”;

$val1 = $con->getMessage($valu);
echo “The Message : $val1<br>”;
}
else
{
echo “Connection established <br>”;
}

$sql=”SELECT id,name FROM login”;
echo “$sql<br>”;

echo “$rs = $con->query($sql);”;
echo “$rs<br>”;

if(DB::isError($rs))
{
die($rs->getMessage());
}

while($row = $rs->fetchRow(DB_FETCHMODE_ASSOC))
{
$fulid = $row[‘id’].” “;
$fulname = $row[‘name’].” “;
echo “Details : $fulid $fulname<br>”;
}
$con->disconnect();
?>

[/QUOTE]

i created a directory name example in /var/www/html
and saved show.php there….

when i am executing this file as
[B][url]http://localhost/example/show.php[/url][/B]

[B]its displaying :[/B] ?

[QUOTE]

The DSN : pgsql://root:@localhost/sample
The Connection Object : Object id #2
Error Number : 1
The Message : DB Error: connect failed
SQL QUERY : SELECT id,name FROM login

[/QUOTE]

i hav tried a lot to find out the error but unable to find it

[I] PostgreSQL version installed in my system :
[/I]
psql (PostgreSQL) 8.0.3
contains support for command-line editing

[I]PHP version installed in my system :[/I]
PHP 5.0.4 (cli) (built: May 9 2005 11:21:11)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.4-dev, Copyright (c) 1998-2004 Zend Technologies

[I]Operating System[/I] : LINUX

Waiting for the REply… ?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@chazzyMar 30.2006 — it looks like you are trying to use the pear module, right? does root have a password?

Have you also looked at the built in PG methods? http://www.php.net/manual/en/ref.pgsql.php
Copy linkTweet thisAlerts:
@manashi_130582authorMar 30.2006 — ya i am useing pear ...

hav tried the following code also...

$dbconn = pg_connect("dbname=sample user=root");

$stat = pg_connection_status($dbconn);

if ($stat === 0)

{

echo 'Connection status ok';

}

else

{

echo 'Connection status bad';

}

$sql="insert into login values('$uname','$enc_pass')";

$rs=pg_query($dbconn, $sql);

if (pg_result_status($rs) === PGSQL_COMMAND_OK)

{

echo 'Looks good to me';

}

else

{

echo pg_result_error($rs);

}
[/QUOTE]

on execution its giving "Connection status ok"

But its not inserting data in the table login

+

its not showing the error message also...

since as per code if query is not getting executed then pg_result_error($rs) should execute...

but only "Connection status ok" message is comming on the page....
Copy linkTweet thisAlerts:
@chazzyMar 30.2006 — (also, instead of code, try using the php tag)

take a look at the error handling features in the pg library.

http://php.net/manual/en/function.pg-last-error.php

so maybe something like this:

[code=php]
$dbconn = pg_connect("dbname=sample user=root");
$stat = pg_connection_status($dbconn);
echo pg_last_error() ."<br />n";
if ($stat === 0)
{
echo 'Connection status ok';
}
else
{
echo 'Connection status bad';
echo pg_last_error() ."<br />n";
}
$sql="insert into login values('$uname','$enc_pass')";
$rs=pg_query($dbconn, $sql);
echo pg_last_error() ."<br />n";
if (pg_result_status($rs) === PGSQL_COMMAND_OK)
{
echo 'Looks good to me';
}
else
{
echo pg_result_error($rs);
}
[/code]
Copy linkTweet thisAlerts:
@manashi_130582authorMar 31.2006 — i hav tried the above code + othe error handling features...

but nothing is gettin displayed on the web page ..

The following message is comeing -

connection :

Connection status ok

The code used was as follows -
<?php

include("DB.php");

$dbconn = pg_connect("dbname=sample user=root");

echo "connection : $dbconn<br>";

echo pg_last_error($dbconn) ."<br />n";

$stat = pg_connection_status($dbconn);

if ($stat == 0)

{

echo "$stat";

echo "Connection status ok";

}

else

{

echo "Connection status bad";

echo pg_last_error($dbconn) ."<br />n";

}

$sql="insert into login values(3,'cc')";

$rs=pg_query($dbconn, $sql);

echo "$rs";

echo pg_last_error($dbconn) ."<br />n";

if (pg_result_status($rs) == PGSQL_COMMAND_OK)

{

echo "Looks good to me";

}

else

{

echo pg_result_error($rs);

}

?>
[/QUOTE]
Copy linkTweet thisAlerts:
@chazzyMar 31.2006 — [code=php]
include("DB.php");
$dbconn = pg_connect("dbname=sample user=root");
[/code]


is this really what you used? the pg_connect is wrong then.
Copy linkTweet thisAlerts:
@manashi_130582authorMar 31.2006 — i hav used the FIRST CODE that i gav in my first MAIL....

the code that u gav in ur mail i just tried to see whether its workin with that or not....
Copy linkTweet thisAlerts:
@chazzyMar 31.2006 — what?

REad this, from php.net
[code=php]
<?php
$dbconn = pg_connect("dbname=mary");
//connect to a database named "mary"

$dbconn2 = pg_connect("host=localhost port=5432 dbname=mary");
// connect to a database named "mary" on "localhost" at port "5432"

$dbconn3 = pg_connect("host=sheep port=5432 dbname=mary user=lamb password=foo");
//connect to a database named "mary" on the host "sheep" with a username and password

$conn_string = "host=sheep port=5432 dbname=test user=lamb password=bar";
$dbconn4 = pg_connect($conn_string);
//connect to a database named "test" on the host "sheep" with a username and password
?>
[/code]


You don't have all of your parameters set.
×

Success!

Help @manashi_130582 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...