Warning: Supplied argument is not a valid PostgreSQL link resource
Hi there
I get the above mentioned warning when I execute php that contacts the
postgre database.
I use the following functions to connect to db
Some of the text is in danish :-)
function aabn_forbindelse_og_vaelgdb() {
$dbcon = pg_connect("host=XXX port=5432 dbname=XXX user=XXX
password=XXX");
if (!$dbcon) {
echo "Kunne ikke oprette en forbindelse til PostGre databasen.";
}
return $dbcon;
}
function luk_forbindelse($dbcon) {
if(!pg_close($dbcon)) {
echo "Kunne ikke lukke forbindelsen til PostGre!";
}
}
function sql_spoerg($dbcon, $query)
{
$resultat = pg_exec($dbcon, $query);
if(!$resultat) {
echo "Kunne ikke udf�re: <em>$query</em>";
}
}
function sql_spoerg_og_faa_svar($dbcon, $query)
{
$resultat = pg_exec($dbcon, $query);
$resultat_array = array();
if(!$resultat)
{
echo "Kunne ikke udf�re: <em>$query</em>";
}
while($raekke = pg_fetch_array($resultat))
{
$resultat_array[] = $raekke;
}
return $resultat_array;
}
Anyone who knows whats wrong ?
jan@klog.dk wrote:
Hi there
I get the above mentioned warning when I execute php that contacts the
postgre database.I use the following functions to connect to db
Some of the text is in danish :-)
...function sql_spoerg_og_faa_svar($dbcon, $query)
{
$resultat = pg_exec($dbcon, $query);
$resultat_array = array();
if(!$resultat)
{
echo "Kunne ikke udføre: <em>$query</em>";
=====>
}
while($raekke = pg_fetch_array($resultat))
{
$resultat_array[] = $raekke;
}
return $resultat_array;
}Anyone who knows whats wrong ?
You are falling through the error and continuing with a bad result handle
up there where I put =====>. Needs a return or something.
I tried to change what lbayuk pointed out.
Now my functions look like this, but I still get the same warning message
<?
global $conn;
function open_conn(){
$conn_string = "host=angua.skjoldhoej.dk port=5432 dbname=fcs user=fcs
password=janfcs";
$conn = pg_connect($conn_string) or die(pg_last_error());
if (!$conn) {
echo "Kunne ikke oprette en forbindelse til PostGre databasen.";
return;
}
}
function close_conn(){
if(!pg_close($conn)) {
echo "Kunne ikke lukke forbindelsen til PostGre!";
return;
}
}
function sql_execute($query)
{
$resultat = pg_exec($conn, $query);
if(!$resultat) {
echo "Kunne ikke udf�re: <em>$query</em>";
return;
}
}
function sql_execute_receive($query)
{
$resultat = pg_exec($conn, $query);
$resultat_array = array();
if(!$resultat)
{
echo "Kunne ikke udf�re: <em>$query</em>";
return;
}
if (pg_numrows($resultat)>0)
{
$row = 0;
while ($item = pg_fetch_array($resultat, $row, PGSQL_ASSOC))
{
$resultat_array[] = $item;
$row++;
}
}
return $resultat_array;
}
?>
"ljb" <lbayuk@mindspring.com> skrev i en meddelelse
news:b3p0m7$amb$1@news.hub.org...
Show quoted text
jan@klog.dk wrote:
Hi there
I get the above mentioned warning when I execute php that contacts the
postgre database.I use the following functions to connect to db
Some of the text is in danish :-)
...function sql_spoerg_og_faa_svar($dbcon, $query)
{
$resultat = pg_exec($dbcon, $query);
$resultat_array = array();
if(!$resultat)
{
echo "Kunne ikke udf�re: <em>$query</em>";=====>
}
while($raekke = pg_fetch_array($resultat))
{
$resultat_array[] = $raekke;
}
return $resultat_array;
}Anyone who knows whats wrong ?
You are falling through the error and continuing with a bad result handle
up there where I put =====>. Needs a return or something.
Hi,
function sql_execute($query)
{
$resultat = pg_exec($conn, $query);if(!$resultat) {
echo "Kunne ikke udføre: <em>$query</em>";
return;
}
}
You are missing the global $conn in the function. You either need to feed it in as a parameter or reference it as a global variable.
function sql_execute($conn, $query)
{
$resultat = pg_exec($conn, $query);
if(!$resultat)
{
echo "Kunne ikke udføre: <em>$query</em>";
return;
}
}
OR
function sql_execute($query)
{
global $conn;
$resultat = pg_exec($conn, $query);
if(!$resultat)
{
echo "Kunne ikke udføre: <em>$query</em>";
return;
}
}
This is the same for the sql_execute_receive, conn_close and any other function that references $conn. PHP does not automatically include global variables in the functions scope.
I currently set $conn to be a global variable (i.e. declared outside the function) and use the global $conn method to get it into scope with the function.
Hope that's a little clearer.
Nick Barr
Import Notes
Resolved by subject fallback
On Sat, 1 Mar 2003, Jan Gravgaard wrote:
I tried to change what lbayuk pointed out.
Now my functions look like this, but I still get the same warning message
<?
global $conn;function open_conn(){
$conn_string = "host=angua.skjoldhoej.dk port=5432 dbname=fcs user=fcs
password=janfcs";
$conn = pg_connect($conn_string) or die(pg_last_error());if (!$conn) {
echo "Kunne ikke oprette en forbindelse til PostGre databasen.";
return;
}
}
Chopping a bit off. A couple of points:
1: your code is formatted in such a way as to be very hard to read. I
don't know if it was the copy and paste into you email client, or if
that's how you indent, but it's very hard for people to follow code that
looks like this does.
2: That said, when you return, you should return the exact var you mean
to return, not rely on php returning the last accessed variable. I've
seen that get people into trouble, so:
return $conn;
instead of return. Then you call it like so:
$my_conn = open_conn();
should get your connect string. Same thing for all your other functions.
Keep in mind, in php global variables aren't global within functions, you
have to explicitly allow them to be seen by your function with the global
keyword, which is discourage by most php developers are a bad way or
writing code.
Hope that helps a bit.