Обсуждение: global variable problem

Поиск
Список
Период
Сортировка

global variable problem

От
"Jules Alberts"
Дата:
hi everybody,

i'm making a little testscript that uses adodb to connect via PHP4 to a
postgresql 7.2 db. something like this:

=== file index.php ===
<?php
include_once('adodb/adodb.inc.php');
$conn=ADONewConnection('postgres7');
if (@$conn->PConnect('localhost','username', 'password', 'dbname')) {
  include("show.php");
}
?>
=== eof ==============

=== file show.php ===
<?php
switch ($action) {
  case start:
    start();
    break;
  case listing:
    listing($bereik);
    break;
  default:
    start();
    break;
}

function start() {
  global $conn;
  echo "<h2>Choose a letter</h2>\n";
  $lett=array(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z);
  foreach ($lett as $i) {
    echo "\n\t<a href='show.php?action=listing&range=$i'>$i</a> ";
  }
}

function listing($range) {
  global $conn;
  echo "<h2>Range: ", $range, "</h2>\n";
  $rs=$conn->Execute("select * from rolo where name like '$range%'");
}
?>
=== eof ==============

that's all. the idea is that the users clicks a letter, the function
listing() is called, and a selection is made. the problem is the
visibility of $conn. it's visible in index.php and in start(), but in
listing() it's gone!?! the line $rs=$conn->exectute in listing()
produces an error "call to a member of a non-object".

what am i doing wrong? how do i make $conn visible in listing()? TIA
for any pointers / help!

--
Jules Alberts

Re: global variable problem

От
"Miguel Carvalho"
Дата:
> 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




Re: global variable problem

От
"Jules Alberts"
Дата:
On 23 Apr 2002 at 10:12, Miguel Carvalho wrote:
> Jules Alberts wrote:
<snip>
> > 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 tried it already with a stored password + user ID. in a global
variable, and guess what: they're not visible. but only in the function
listing(). in the function start() they are both visible...

> >
> > 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 ).

i have a feeling it's more complex than that. a script calls itself,
rebuilds HTML code. has the script ended or not? the script that did
the call has ended. but since it has called itself, it hasn't. i find
these recursion matters a bit vague when it comes to scipts that
generate themselves.

> > -    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:

i did that, it doesn't make any difference

> > - 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 don't think that would be a problem, as long as you explicitely do a
begin /end transaction.

i tried the original script in a more unmodified way (it comes from the
latest Dutch issue of C't magazine), but it has the same problem. this
made me think that the problem is maybe in some setting in php.ini or
httpd.conf. i'm going to do a complete reinstall of apache and php. if
the problem persists, i will try to make the script as small as
possible while still illustrating the problem.

so, maybe until later, and thanks for your reaction.

--
Jules Alberts

Re: global variable problem

От
"Papp, Gyozo"
Дата:
hi,


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

in general this is the case. If you must do somethign after all output sent to
the browser you can register one or more shutdown function to process in hide.
But it's not so common.


| i have a feeling it's more complex than that. a script calls itself,
| rebuilds HTML code. has the script ended or not? the script that did

It's up to at all.

| the call has ended. but since it has called itself, it hasn't. i find
| these recursion matters a bit vague when it comes to scipts that
| generate themselves.


Tere is no recursion, it's  a different thing. You can avoid it with some
so called state variable.

switch ($status){
case 'BUILD_FORM_FIRST':
case 'CHECK_FORM':
case 'SHOW_RESULT':
}

|
| > > - 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:
|
| i did that, it doesn't make any difference


I'm afraid that you didn't reopen the connection in show.php.
you must initialize each variable in each separate page, because each script
is a totally independent program from each other. SESSION can ease your work
but resources such as connection, result sets, cannot be registered in a
session.

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

No, at the end of the script PHP sends a ROLLBACK automatically to
postgres. So it can rollback the queries in the actual request (page) only.




Re: global variable problem

От
"Jules Alberts"
Дата:
On 23 Apr 2002 at 12:27, Jules Alberts wrote:
<big snip>
> so, maybe until later, and thanks for your reaction.

google was my friend. it's a bug, plain and simple.

http://www.geocrawler.com/mail/msg_raw.php3?msg_id=7208901

i run 4.0.6, the latest official redhat RPMs. i'm downloading the
sources to 4.2.0 now (no RPMs yet, only some 4.2.0 release candidates),
i hope it will help and the installation will work.

thanks for your reactions everyone.

--
Jules Alberts

Re: global variable problem

От
Chris
Дата:
Hi,

>| > doing a roolback in this connection, doest it will roolback all scripts?
>
>No, at the end of the script PHP sends a ROLLBACK automatically to
>postgres. So it can rollback the queries in the actual request (page) only.

I hope not!

If you begin a transaction in your PHP code explicitly, then it's the same
as manually running the commands from the psql monitor, so you need to
explicitly say "COMMIT" or "ROLLBACK".

Remember that postgres automatically wraps everything in transactions, so
unless you explicitly start one, you can't do a rollback from a previous
update / insert query (because this is a different transaction).


-----------------
      Chris Smith
http://www.squiz.net/


Re: global variable problem

От
"Papp, Gyozo"
Дата:
hi,

| >| > doing a roolback in this connection, doest it will roolback all scripts?
| >
| >No, at the end of the script PHP sends a ROLLBACK automatically to
| >postgres. So it can rollback the queries in the actual request (page) only.
|
| I hope not!

I meant if you start a transaction (BEGIN) but you didn't commit it till
the end of page. Try it!