Re: How to pass connection as global variable ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thanks M. Sokolewicz
for explaining the "under water" basics ...
Cor

----- Original Message ----- From: "M. Sokolewicz" <tularis@xxxxxxx>
To: "C.R.Vegelin" <cr.vegelin@xxxxxxxxx>
Cc: "Chris" <dmagick@xxxxxxxxx>; "php-general@xxxxxxxxxxxx" <php-general@xxxxxxxxxxxxx>
Sent: Wednesday, July 11, 2007 9:56 AM
Subject: Re:  How to pass connection as global variable ?


C.R.Vegelin wrote:
C.R.Vegelin wrote:
I have various PHP scripts that use the same database.
The startup script default.php sets the connection once for all the scripts. This connection is set in $_SESSION to make it a global variable for all scripts. When switching from page default to page faq, I get errors I can't explain. Any help is highly appreciated.
TIA, Cor
 default.php
----------------
<?php
session_start();
require("menu.php");

<snip>

faq.php
-----------
<?php require("menu.php");

The first two lines give it away ;)

You're missing a session_start().


--
Postgresql & php tutorials
http://www.designmagick.com/


Thanks Chris,

I included session_start(); as first line in faq.php, but now I get other warnings /errors:
Warning: mysqli_query() [function.mysqli-query]: Couldn't fetch mysqli
   in line "$result = mysqli_query($link, $sql);"
Warning: mysqli_error() [function.mysqli-error]: Couldn't fetch mysqli
in line "if (!$result) { echo "Can't execute query ($sql): " . mysqli_error($link); exit; }

Actually - shoot me for not noticing this before.

You can't store a resource or connection in the session.

You have to recreate your connection in each page.

See http://php.net/session .

--
Postgresql & php tutorials
http://www.designmagick.com/


Hi Chris,

I recreated a new connection in faq.php and it's working now.
I had the impression that 1 connection could last during a user session,
but apparently a user session may need many connections.

Thanks again, Cor

Remember that the http protocol is stateless. Every request made is a new connection to the server. It's due to extra technologies like cookies (and in this case sessions) that some form of 'state' can be maintained. However, because there is no communication between client and server unless the client makes a request you can never actually determine the _current_ status of your session, only in retrospect you can. If a user makes a request, you could say the session is 'in progress', however, after sending that page you don't know if it's still 'in progress' or has ended until you recieve another request, thus telling you it WAS still in progress.

Now, as for resources and connections: PHP clears up everything when it finishes sending a response to a request. This does not happen for certain very specific items which were stored away (like in a database or in a session, which is usually just a file). For a connection to be 'open' it needs to be able to communicate with both its' sides (client: your database; and the 'server': PHP), if php 'shuts down' because it finished the request, it doesn't leave anything of itself behind, and as such the 'server' of that connection also disappears, thus creating a broken link which is automatically destroyed. Of course the php engine is smart and knows this would happen, so it 'cleanly' closes the connection before disappearing to reappear again (fresh) on a next request.

hope that helps you understand,
- Tul



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux