Re: Warning: Supplied argument is not a valid PostgreSQL

From: "scott(dot)marlowe" <scott(dot)marlowe(at)ihs(dot)com>
To: Jan Gravgaard <jan(at)klog(dot)dk>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Warning: Supplied argument is not a valid PostgreSQL
Date: 2003-03-03 18:11:11
Message-ID: Pine.LNX.4.33.0303031107490.12833-100000@css120.ihs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message scott.marlowe 2003-03-03 18:22:31 Re: Hwo to set timeout for PHP persistent connections
Previous Message scott.marlowe 2003-03-03 18:06:20 Re: Q from new user about postgresql?