Re: global variable problem

From: "Miguel Carvalho" <miguel(at)ipatimup(dot)pt>
To: <jules(dot)alberts(at)arbodienst-limburg(dot)nl>
Cc: <pgsql-php(at)postgresql(dot)org>
Subject: Re: global variable problem
Date: 2002-04-23 09:12:41
Message-ID: 8298.193.136.62.21.1019553161.squirrel@www.ipatimup.up.pt
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-php

> On 22 Apr 2002 at 15:47, Miguel Carvalho wrote:
>>
>> >
>> > what am i doing wrong? how do i make $conn visible in listing()? TIA
>> > for any pointers / help!
>>
>> The problem is when the script ends the connection to the database is
>> lost( this is normal ).
>> To avoid this, you have to establish a connection on each php script
>> that needs to use the database.
>> Dont try to keep the connection as a session variable....it doesnt
>> work.
>
> hello Miguel,
>
> i used a sample script as an example to build this code. in the
> example, in every function a new connection is made (like you suggest).

> i tried to avoid this, i guessed it would be a lot of extra overhead.

You are right about the overhead.

> besides, my db requires a password so i would have to store it
> somewhere in a variable, which seems a bit of a security risk.

Where are you going to store the login and database password?
I suggest you to store the login and password in a session variable ( take
a lookt at session_register ). I think that is the best way to do it.

>
> i will try this approach anyway, but i still have a few questions
>

> - when does a script end? at the end of the .PHP file? after a call is
> made to another .PHP file?

The script ends when the .php file reach it's end( when there is noting
more to parse/ and or execute ).

> - why is the variable $conn visible in start(), but not in listing()?

If you want to have access to a global var. inside of a function, you must
say that the global var. is global.
Ex:
global $name;

print "The name is: $name";

function display()
{

global $name; //if you ommit this line, php think's that $name is a
local variable
print "The name is: $name";
}

>
> - shouldn't a _P_connect create a persistent connection?

Possibly it would be a good solution...but there may be some problems, like:

doing a roolback in this connection, doest it will roolback all scripts?

I dont know the answer. If any one know's, please let us know.

>
> sorry if this is all very basic, i'm a complete PHP newbie :)
>

Some time ago a was a newbie to :)

Regards
Miguel Carvalho

Responses

Browse pgsql-php by date

  From Date Subject
Next Message Jules Alberts 2002-04-23 10:27:00 Re: global variable problem
Previous Message Papp, Gyozo 2002-04-22 22:53:20 Re: RV: calling a pl/pgsql function with array in argument