instead of making it global, pass the object when you call the function, or 
make a function which returns your connection object...ie

in one file...

function connect($user, $pass)
{
//create connection resource here

return $connection_resource
}

in your other file...

function getTodaysImages() {

//grab your connection resource
$connection = connect($user, pass);

//MAKE SURE you included the file where your connection code is
Include 'example.php';

$query = "SELECT * FROM MyTable where date_last_viewed = cast('" .
 date("j F Y") . "' as date);";
//echo($query . "<hr>");
$result = pg_query($connection, $query) or die("Error in query: $query. "
 . pg_last_error($connection));

                return $result;
}


HTH!

On August 23, 2003 03:25 am, you wrote:
> It's late, so maybe I'm missing something simple, but here goes...
>
> I have a php file that has this code:
>
>       function getTodaysImages() {
>               $query = "SELECT * FROM MyTable where date_last_viewed = cast('" .
> date("j F Y") . "' as date);";
>               //echo($query . "<hr>");
>               $result = pg_query($connection, $query) or die("Error in query: 
> $query. "
> . pg_last_error($connection));
>
>               return $result;
>       }
>
> the $connection object is declared in a previous function with the global
> keyword.  My understanding is that it should then be available to
> subsequent functions.  When I run my code, I get this error:
>
> Warning: pg_exec(): supplied argument is not a valid PostgreSQL link
> resource in /home/httpd/htdocs/myfolder/db.php on line 23
>
> Line 23 happens to be the pg_query line.  If I take this code and put it
> directly on my web page (instead of through a function in an include file),
> I get no errors.  So it would appear I have a variable scoping issue.  The
> resources I have thus far (including Google) haven't been too helpful - I
> haven't found that "magic" search term yet.  (searching on the error
> returns a crap load of files which suffer from the same error, rather than
> a discussion of the error - found one discussion, but the answer didn't
> seem to apply and didn't fix it anyways.. )
>
> Anyone have any tips?  I am new to PHP and Postgresql (finally got postgres
> running yesterday...), and am at the beginning of the learning process with
> both...
>
> Thanks bunchs for any input.
>
> Shawn

-- 
Nick W ([EMAIL PROTECTED])
Registered Linux User #324288 (http://counter.li.org)
MSN Messenger: [EMAIL PROTECTED]
Yahoo: foolish_gambit
ICQ: 303276221
Website: www.fromthecrosstothethrone.com

Reply via email to