losing MySQL resource
am 19.09.2009 12:11:06 von Stan
I'm maintaining a session. I successfully connect to a database ($DBConnect
= mysql_connect()). I save the connection resource in a session variable
($_SESSION['connection'] = $DBConnect) ... to use in subsequent queries. It
remains valid while the user is on the current page.
print_r($_SESSION['connection']) yields 'Resource id #3', for instance.
User browses to a new page ... in the same session. $_SESSION['$DBConnect']
no longer references a valid mysql resource.
print_r($_SESSION['connection']) yields '0'.
Other "ordinary" values saved in $_SESSION variables remain valid.
Is there something special about a mysql resource?
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: losing MySQL resource
am 19.09.2009 13:25:36 von Niel Archer
> I'm maintaining a session. I successfully connect to a database ($DBConnect
> = mysql_connect()). I save the connection resource in a session variable
> ($_SESSION['connection'] = $DBConnect) ... to use in subsequent queries. It
> remains valid while the user is on the current page.
> print_r($_SESSION['connection']) yields 'Resource id #3', for instance.
>
> User browses to a new page ... in the same session. $_SESSION['$DBConnect']
> no longer references a valid mysql resource.
> print_r($_SESSION['connection']) yields '0'.
>
> Other "ordinary" values saved in $_SESSION variables remain valid.
>
> Is there something special about a mysql resource?
Not about the resource itself, no. PHP closes connections to a Db when a
script ends, unless the connection is persistent, so while the resource
IS saved, the connection to which it refers no longer exists.
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
Niel Archer
niel.archer (at) blueyonder.co.uk
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: losing MySQL resource
am 19.09.2009 22:24:21 von Stan
Thanks.
"Niel Archer" wrote in message
news:20090919121842.B95B.A5CB2C2A@chance.now...
> > I'm maintaining a session. I successfully connect to a database
($DBConnect
> > = mysql_connect()). I save the connection resource in a session
variable
> > ($_SESSION['connection'] = $DBConnect) ... to use in subsequent queries.
It
> > remains valid while the user is on the current page.
> > print_r($_SESSION['connection']) yields 'Resource id #3', for instance.
> >
> > User browses to a new page ... in the same session.
$_SESSION['$DBConnect']
> > no longer references a valid mysql resource.
> > print_r($_SESSION['connection']) yields '0'.
> >
> > Other "ordinary" values saved in $_SESSION variables remain valid.
> >
> > Is there something special about a mysql resource?
>
> Not about the resource itself, no. PHP closes connections to a Db when a
> script ends, unless the connection is persistent, so while the resource
> IS saved, the connection to which it refers no longer exists.
>
> >
> > --
> > PHP Database Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
> --
> Niel Archer
> niel.archer (at) blueyonder.co.uk
>
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: losing MySQL resource
am 21.09.2009 00:49:16 von dmagick
Niel Archer wrote:
>> I'm maintaining a session. I successfully connect to a database ($DBConnect
>> = mysql_connect()). I save the connection resource in a session variable
>> ($_SESSION['connection'] = $DBConnect) ... to use in subsequent queries. It
>> remains valid while the user is on the current page.
>> print_r($_SESSION['connection']) yields 'Resource id #3', for instance.
>>
>> User browses to a new page ... in the same session. $_SESSION['$DBConnect']
>> no longer references a valid mysql resource.
>> print_r($_SESSION['connection']) yields '0'.
>>
>> Other "ordinary" values saved in $_SESSION variables remain valid.
>>
>> Is there something special about a mysql resource?
>
> Not about the resource itself, no. PHP closes connections to a Db when a
> script ends, unless the connection is persistent, so while the resource
> IS saved, the connection to which it refers no longer exists.
No resources (whether they are persistent or not) can be stored in the
session.
http://www.php.net/manual/en/intro.session.php
The big pink box has more info.
--
Postgresql & php tutorials
http://www.designmagick.com/
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: losing MySQL resource
am 09.11.2009 17:46:57 von Stan
How do I make an Object persistant for the duration of a Session?
Thanks,
"Chris" wrote in message news:4AB6B16C.8@gmail.com...
> Niel Archer wrote:
> >> I'm maintaining a session. I successfully connect to a database
($DBConnect
> >> = mysql_connect()). I save the connection resource in a session
variable
> >> ($_SESSION['connection'] = $DBConnect) ... to use in subsequent
queries. It
> >> remains valid while the user is on the current page.
> >> print_r($_SESSION['connection']) yields 'Resource id #3', for instance.
> >>
> >> User browses to a new page ... in the same session.
$_SESSION['$DBConnect']
> >> no longer references a valid mysql resource.
> >> print_r($_SESSION['connection']) yields '0'.
> >>
> >> Other "ordinary" values saved in $_SESSION variables remain valid.
> >>
> >> Is there something special about a mysql resource?
> >
> > Not about the resource itself, no. PHP closes connections to a Db when a
> > script ends, unless the connection is persistent, so while the resource
> > IS saved, the connection to which it refers no longer exists.
>
> No resources (whether they are persistent or not) can be stored in the
> session.
>
> http://www.php.net/manual/en/intro.session.php
>
> The big pink box has more info.
>
> --
> Postgresql & php tutorials
> http://www.designmagick.com/
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: losing MySQL resource
am 09.11.2009 18:54:25 von andy-lists
Hi,
I got around this by creating a database wrapper class which gets
passed the credentials from the app's config file. An instance of the
class is created and saved in the session, and every query to the
database runs through the class's Query() wrapper method which checks
if the connection is alive and valid - if it isn't, it reconnects it
before running the query.
Andy
On 9 November2009, at 16:46, Stan wrote:
> How do I make an Object persistant for the duration of a Session?
> Thanks,
> "Chris" wrote in message news:4AB6B16C.
> 8@gmail.com...
>> Niel Archer wrote:
>>>> I'm maintaining a session. I successfully connect to a database
> ($DBConnect
>>>> = mysql_connect()). I save the connection resource in a session
> variable
>>>> ($_SESSION['connection'] = $DBConnect) ... to use in subsequent
> queries. It
>>>> remains valid while the user is on the current page.
>>>> print_r($_SESSION['connection']) yields 'Resource id #3', for
>>>> instance.
>>>>
>>>> User browses to a new page ... in the same session.
> $_SESSION['$DBConnect']
>>>> no longer references a valid mysql resource.
>>>> print_r($_SESSION['connection']) yields '0'.
>>>>
>>>> Other "ordinary" values saved in $_SESSION variables remain valid.
>>>>
>>>> Is there something special about a mysql resource?
>>>
>>> Not about the resource itself, no. PHP closes connections to a Db
>>> when a
>>> script ends, unless the connection is persistent, so while the
>>> resource
>>> IS saved, the connection to which it refers no longer exists.
>>
>> No resources (whether they are persistent or not) can be stored in
>> the
>> session.
>>
>> http://www.php.net/manual/en/intro.session.php
>>
>> The big pink box has more info.
>>
>> --
>> Postgresql & php tutorials
>> http://www.designmagick.com/
>>
>
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: losing MySQL resource
am 09.11.2009 22:11:11 von Stan
I got as far as creating a class to be the wrapper ... instantiating an
object of that class ... saving the reference in a $_SESSION variable ...
but my object reference seems to be invalid upon the next request (in the
same session) from the client browser. What have I missed?
Thanks,
Stan
""Andy Shellam (Mailing Lists)"" wrote in
message news:FD8200B0-E18A-4AFD-8FFC-F51080621BB4@networkmail.eu...
> Hi,
>
> I got around this by creating a database wrapper class which gets
> passed the credentials from the app's config file. An instance of the
> class is created and saved in the session, and every query to the
> database runs through the class's Query() wrapper method which checks
> if the connection is alive and valid - if it isn't, it reconnects it
> before running the query.
>
> Andy
>
> On 9 November2009, at 16:46, Stan wrote:
>
> > How do I make an Object persistant for the duration of a Session?
> > Thanks,
> > "Chris" wrote in message news:4AB6B16C.
> > 8@gmail.com...
> >> Niel Archer wrote:
> >>>> I'm maintaining a session. I successfully connect to a database
> > ($DBConnect
> >>>> = mysql_connect()). I save the connection resource in a session
> > variable
> >>>> ($_SESSION['connection'] = $DBConnect) ... to use in subsequent
> > queries. It
> >>>> remains valid while the user is on the current page.
> >>>> print_r($_SESSION['connection']) yields 'Resource id #3', for
> >>>> instance.
> >>>>
> >>>> User browses to a new page ... in the same session.
> > $_SESSION['$DBConnect']
> >>>> no longer references a valid mysql resource.
> >>>> print_r($_SESSION['connection']) yields '0'.
> >>>>
> >>>> Other "ordinary" values saved in $_SESSION variables remain valid.
> >>>>
> >>>> Is there something special about a mysql resource?
> >>>
> >>> Not about the resource itself, no. PHP closes connections to a Db
> >>> when a
> >>> script ends, unless the connection is persistent, so while the
> >>> resource
> >>> IS saved, the connection to which it refers no longer exists.
> >>
> >> No resources (whether they are persistent or not) can be stored in
> >> the
> >> session.
> >>
> >> http://www.php.net/manual/en/intro.session.php
> >>
> >> The big pink box has more info.
> >>
> >> --
> >> Postgresql & php tutorials
> >> http://www.designmagick.com/
> >>
> >
> >
> >
> > --
> > PHP Database Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: losing MySQL resource
am 09.11.2009 22:19:03 von andy-lists
Hi Stan,
Are you saving the instance of your class in $_SESSION?
Class instances can be persisted across sessions, however resources
(i.e. MySQL and other DB connections) cannot. What you need to do, is
every method in your wrapper class that uses the MySQL resource needs
to check if it's a valid resource (is_resource() springs to mind.)
If it isn't valid, you need to re-create it (using mysql_connect).
The reason for this is that PHP isn't running between session
requests, so it cannot maintain and monitor the database connection
across requests.
Andy
On 9 November2009, at 21:11, Stan wrote:
> I got as far as creating a class to be the wrapper ... instantiating
> an
> object of that class ... saving the reference in a $_SESSION
> variable ...
> but my object reference seems to be invalid upon the next request
> (in the
> same session) from the client browser. What have I missed?
>
> Thanks,
> Stan
>
> ""Andy Shellam (Mailing Lists)"" wrote in
> message news:FD8200B0-E18A-4AFD-8FFC-F51080621BB4@networkmail.eu...
>> Hi,
>>
>> I got around this by creating a database wrapper class which gets
>> passed the credentials from the app's config file. An instance of
>> the
>> class is created and saved in the session, and every query to the
>> database runs through the class's Query() wrapper method which checks
>> if the connection is alive and valid - if it isn't, it reconnects it
>> before running the query.
>>
>> Andy
>>
>> On 9 November2009, at 16:46, Stan wrote:
>>
>>> How do I make an Object persistant for the duration of a Session?
>>> Thanks,
>>> "Chris" wrote in message news:4AB6B16C.
>>> 8@gmail.com...
>>>> Niel Archer wrote:
>>>>>> I'm maintaining a session. I successfully connect to a database
>>> ($DBConnect
>>>>>> = mysql_connect()). I save the connection resource in a session
>>> variable
>>>>>> ($_SESSION['connection'] = $DBConnect) ... to use in subsequent
>>> queries. It
>>>>>> remains valid while the user is on the current page.
>>>>>> print_r($_SESSION['connection']) yields 'Resource id #3', for
>>>>>> instance.
>>>>>>
>>>>>> User browses to a new page ... in the same session.
>>> $_SESSION['$DBConnect']
>>>>>> no longer references a valid mysql resource.
>>>>>> print_r($_SESSION['connection']) yields '0'.
>>>>>>
>>>>>> Other "ordinary" values saved in $_SESSION variables remain
>>>>>> valid.
>>>>>>
>>>>>> Is there something special about a mysql resource?
>>>>>
>>>>> Not about the resource itself, no. PHP closes connections to a Db
>>>>> when a
>>>>> script ends, unless the connection is persistent, so while the
>>>>> resource
>>>>> IS saved, the connection to which it refers no longer exists.
>>>>
>>>> No resources (whether they are persistent or not) can be stored in
>>>> the
>>>> session.
>>>>
>>>> http://www.php.net/manual/en/intro.session.php
>>>>
>>>> The big pink box has more info.
>>>>
>>>> --
>>>> Postgresql & php tutorials
>>>> http://www.designmagick.com/
>>>>
>>>
>>>
>>>
>>> --
>>> PHP Database Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>>
>>
>
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: losing MySQL resource
am 11.11.2009 14:15:00 von Stan
OK. The secret, as has been stated here before, is to load the class
definitions BEFORE calling session_start(). Thanks to everyone.
""Andy Shellam (Mailing Lists)"" wrote in
message news:3339C510-BE08-4426-9265-BA1965A9ADDF@networkmail.eu...
> Hi Stan,
>
> Are you saving the instance of your class in $_SESSION?
>
> Class instances can be persisted across sessions, however resources
> (i.e. MySQL and other DB connections) cannot. What you need to do, is
> every method in your wrapper class that uses the MySQL resource needs
> to check if it's a valid resource (is_resource() springs to mind.)
>
> If it isn't valid, you need to re-create it (using mysql_connect).
>
> The reason for this is that PHP isn't running between session
> requests, so it cannot maintain and monitor the database connection
> across requests.
>
> Andy
>
> On 9 November2009, at 21:11, Stan wrote:
>
> > I got as far as creating a class to be the wrapper ... instantiating
> > an
> > object of that class ... saving the reference in a $_SESSION
> > variable ...
> > but my object reference seems to be invalid upon the next request
> > (in the
> > same session) from the client browser. What have I missed?
> >
> > Thanks,
> > Stan
> >
> > ""Andy Shellam (Mailing Lists)"" wrote in
> > message news:FD8200B0-E18A-4AFD-8FFC-F51080621BB4@networkmail.eu...
> >> Hi,
> >>
> >> I got around this by creating a database wrapper class which gets
> >> passed the credentials from the app's config file. An instance of
> >> the
> >> class is created and saved in the session, and every query to the
> >> database runs through the class's Query() wrapper method which checks
> >> if the connection is alive and valid - if it isn't, it reconnects it
> >> before running the query.
> >>
> >> Andy
> >>
> >> On 9 November2009, at 16:46, Stan wrote:
> >>
> >>> How do I make an Object persistant for the duration of a Session?
> >>> Thanks,
> >>> "Chris" wrote in message news:4AB6B16C.
> >>> 8@gmail.com...
> >>>> Niel Archer wrote:
> >>>>>> I'm maintaining a session. I successfully connect to a database
> >>> ($DBConnect
> >>>>>> = mysql_connect()). I save the connection resource in a session
> >>> variable
> >>>>>> ($_SESSION['connection'] = $DBConnect) ... to use in subsequent
> >>> queries. It
> >>>>>> remains valid while the user is on the current page.
> >>>>>> print_r($_SESSION['connection']) yields 'Resource id #3', for
> >>>>>> instance.
> >>>>>>
> >>>>>> User browses to a new page ... in the same session.
> >>> $_SESSION['$DBConnect']
> >>>>>> no longer references a valid mysql resource.
> >>>>>> print_r($_SESSION['connection']) yields '0'.
> >>>>>>
> >>>>>> Other "ordinary" values saved in $_SESSION variables remain
> >>>>>> valid.
> >>>>>>
> >>>>>> Is there something special about a mysql resource?
> >>>>>
> >>>>> Not about the resource itself, no. PHP closes connections to a Db
> >>>>> when a
> >>>>> script ends, unless the connection is persistent, so while the
> >>>>> resource
> >>>>> IS saved, the connection to which it refers no longer exists.
> >>>>
> >>>> No resources (whether they are persistent or not) can be stored in
> >>>> the
> >>>> session.
> >>>>
> >>>> http://www.php.net/manual/en/intro.session.php
> >>>>
> >>>> The big pink box has more info.
> >>>>
> >>>> --
> >>>> Postgresql & php tutorials
> >>>> http://www.designmagick.com/
> >>>>
> >>>
> >>>
> >>>
> >>> --
> >>> PHP Database Mailing List (http://www.php.net/)
> >>> To unsubscribe, visit: http://www.php.net/unsub.php
> >>>
> >>>
> >>
> >
> >
> >
> > --
> > PHP Database Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: losing MySQL resource
am 11.11.2009 15:25:07 von Nehemias Duarte
I was under the impression that session_start() had to be the FIRST
thing ran in the script. Is this incorrect?
>>OK. The secret, as has been stated here before, is to load the class
definitions BEFORE calling session_start(). Thanks to everyone.
""Andy Shellam (Mailing Lists)"" wrote in
message news:3339C510-BE08-4426-9265-BA1965A9ADDF@networkmail.eu...
> Hi Stan,
>
> Are you saving the instance of your class in $_SESSION?
>
> Class instances can be persisted across sessions, however resources
> (i.e. MySQL and other DB connections) cannot. What you need to do, is
> every method in your wrapper class that uses the MySQL resource needs
> to check if it's a valid resource (is_resource() springs to mind.)
>
> If it isn't valid, you need to re-create it (using mysql_connect).
>
> The reason for this is that PHP isn't running between session
> requests, so it cannot maintain and monitor the database connection
> across requests.
>
> Andy
>
> On 9 November2009, at 21:11, Stan wrote:
>
> > I got as far as creating a class to be the wrapper ... instantiating
> > an
> > object of that class ... saving the reference in a $_SESSION
> > variable ...
> > but my object reference seems to be invalid upon the next request
> > (in the
> > same session) from the client browser. What have I missed?
> >
> > Thanks,
> > Stan
> >
> > ""Andy Shellam (Mailing Lists)"" wrote
in
> > message news:FD8200B0-E18A-4AFD-8FFC-F51080621BB4@networkmail.eu...
> >> Hi,
> >>
> >> I got around this by creating a database wrapper class which gets
> >> passed the credentials from the app's config file. An instance of
> >> the
> >> class is created and saved in the session, and every query to the
> >> database runs through the class's Query() wrapper method which
checks
> >> if the connection is alive and valid - if it isn't, it reconnects
it
> >> before running the query.
> >>
> >> Andy
> >>
> >> On 9 November2009, at 16:46, Stan wrote:
> >>
> >>> How do I make an Object persistant for the duration of a Session?
> >>> Thanks,
> >>> "Chris" wrote in message news:4AB6B16C.
> >>> 8@gmail.com...
> >>>> Niel Archer wrote:
> >>>>>> I'm maintaining a session. I successfully connect to a
database
> >>> ($DBConnect
> >>>>>> =3D mysql_connect()). I save the connection resource in a
session
> >>> variable
> >>>>>> ($_SESSION['connection'] =3D $DBConnect) ... to use in =
subsequent
> >>> queries. It
> >>>>>> remains valid while the user is on the current page.
> >>>>>> print_r($_SESSION['connection']) yields 'Resource id #3', for
> >>>>>> instance.
> >>>>>>
> >>>>>> User browses to a new page ... in the same session.
> >>> $_SESSION['$DBConnect']
> >>>>>> no longer references a valid mysql resource.
> >>>>>> print_r($_SESSION['connection']) yields '0'.
> >>>>>>
> >>>>>> Other "ordinary" values saved in $_SESSION variables remain
> >>>>>> valid.
> >>>>>>
> >>>>>> Is there something special about a mysql resource?
> >>>>>
> >>>>> Not about the resource itself, no. PHP closes connections to a
Db
> >>>>> when a
> >>>>> script ends, unless the connection is persistent, so while the
> >>>>> resource
> >>>>> IS saved, the connection to which it refers no longer exists.
> >>>>
> >>>> No resources (whether they are persistent or not) can be stored
in
> >>>> the
> >>>> session.
> >>>>
> >>>> http://www.php.net/manual/en/intro.session.php
> >>>>
> >>>> The big pink box has more info.
> >>>>
> >>>> --=20
> >>>> Postgresql & php tutorials
> >>>> http://www.designmagick.com/
> >>>>
> >>>
> >>>
> >>>
> >>> --=20
> >>> PHP Database Mailing List (http://www.php.net/)
> >>> To unsubscribe, visit: http://www.php.net/unsub.php
> >>>
> >>>
> >>
> >
> >
> >
> > --=20
> > PHP Database Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
--=20
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Confidentiality Notice:=20
This e-mail and any attachments may contain confidential information =
intended solely for the use of the addressee. If the reader of this =
message is not the intended recipient, any distribution, copying, or use =
of this e-mail or its attachments is prohibited. If you received this =
message in error, please notify the sender immediately by e-mail and =
delete this message and any copies. Thank you.
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: losing MySQL resource
am 11.11.2009 16:31:08 von M.Ford
> -----Original Message-----
> From: Nehemias Duarte [mailto:NDuarte@aflac.com]
> Sent: 11 November 2009 14:25
> To: Stan; php-db@lists.php.net
> Subject: RE: [PHP-DB] losing MySQL resource
>=20
> I was under the impression that session_start() had to be the FIRST
> thing ran in the script. Is this incorrect?
Yes.
It has to be before any actual *output*, but it would be perfectly valid to=
have umpteen thousand lines of PHP before session_start() so long as all t=
he output came after it.
Cheers!
Mike
--=20
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507, Civic Quarter Campus,=20
Woodhouse Lane, LEEDS,=A0 LS1 3HE,=A0 United Kingdom=20
Email: m.ford@leedsmet.ac.uk=20
Tel: +44 113 812 4730
To view the terms under which this email is distributed, please go to http:=
//disclaimer.leedsmet.ac.uk/email.htm
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: losing MySQL resource
am 12.11.2009 12:12:00 von Stan
I don't understand the process. When session_start() reconnects to a (the)
session, objects saved as $_SESSION variables must be "recreated". To do
this requires the class definition. So the class definition must be loaded
prior to calling session_start().
Some other languages I've written in had a process called serialization
through which objects were saved. And an opposite (deserialization?)
process through which objects were restored. To have "persistent" objects
requires some way to "save" the objects for the next invocation of the
application (PHP in this case). Deserialization necessarily requires the
template upon which the original object was built (the class definition).
I found http://us.php.net/serialize in the online PHP manual. I also found
http://us.php.net/manual/en/language.oop5.autoload.php in the online PHP
manual (I have not tried autoload).
I simply moved the require_once("class_definitions.inc"); to appear
immediately before my session_start(); and everything worked thereafter.
"Nehemias Duarte" wrote in message
news:2A10E35796E8A84FB782111FE5D3B30A03604B@PCMEX02.hq.aflac .com...
I was under the impression that session_start() had to be the FIRST
thing ran in the script. Is this incorrect?
>>OK. The secret, as has been stated here before, is to load the class
definitions BEFORE calling session_start(). Thanks to everyone.
""Andy Shellam (Mailing Lists)"" wrote in
message news:3339C510-BE08-4426-9265-BA1965A9ADDF@networkmail.eu...
> Hi Stan,
>
> Are you saving the instance of your class in $_SESSION?
>
> Class instances can be persisted across sessions, however resources
> (i.e. MySQL and other DB connections) cannot. What you need to do, is
> every method in your wrapper class that uses the MySQL resource needs
> to check if it's a valid resource (is_resource() springs to mind.)
>
> If it isn't valid, you need to re-create it (using mysql_connect).
>
> The reason for this is that PHP isn't running between session
> requests, so it cannot maintain and monitor the database connection
> across requests.
>
> Andy
>
> On 9 November2009, at 21:11, Stan wrote:
>
> > I got as far as creating a class to be the wrapper ... instantiating
> > an
> > object of that class ... saving the reference in a $_SESSION
> > variable ...
> > but my object reference seems to be invalid upon the next request
> > (in the
> > same session) from the client browser. What have I missed?
> >
> > Thanks,
> > Stan
> >
> > ""Andy Shellam (Mailing Lists)"" wrote
in
> > message news:FD8200B0-E18A-4AFD-8FFC-F51080621BB4@networkmail.eu...
> >> Hi,
> >>
> >> I got around this by creating a database wrapper class which gets
> >> passed the credentials from the app's config file. An instance of
> >> the
> >> class is created and saved in the session, and every query to the
> >> database runs through the class's Query() wrapper method which
checks
> >> if the connection is alive and valid - if it isn't, it reconnects
it
> >> before running the query.
> >>
> >> Andy
> >>
> >> On 9 November2009, at 16:46, Stan wrote:
> >>
> >>> How do I make an Object persistant for the duration of a Session?
> >>> Thanks,
> >>> "Chris" wrote in message news:4AB6B16C.
> >>> 8@gmail.com...
> >>>> Niel Archer wrote:
> >>>>>> I'm maintaining a session. I successfully connect to a
database
> >>> ($DBConnect
> >>>>>> = mysql_connect()). I save the connection resource in a
session
> >>> variable
> >>>>>> ($_SESSION['connection'] = $DBConnect) ... to use in subsequent
> >>> queries. It
> >>>>>> remains valid while the user is on the current page.
> >>>>>> print_r($_SESSION['connection']) yields 'Resource id #3', for
> >>>>>> instance.
> >>>>>>
> >>>>>> User browses to a new page ... in the same session.
> >>> $_SESSION['$DBConnect']
> >>>>>> no longer references a valid mysql resource.
> >>>>>> print_r($_SESSION['connection']) yields '0'.
> >>>>>>
> >>>>>> Other "ordinary" values saved in $_SESSION variables remain
> >>>>>> valid.
> >>>>>>
> >>>>>> Is there something special about a mysql resource?
> >>>>>
> >>>>> Not about the resource itself, no. PHP closes connections to a
Db
> >>>>> when a
> >>>>> script ends, unless the connection is persistent, so while the
> >>>>> resource
> >>>>> IS saved, the connection to which it refers no longer exists.
> >>>>
> >>>> No resources (whether they are persistent or not) can be stored
in
> >>>> the
> >>>> session.
> >>>>
> >>>> http://www.php.net/manual/en/intro.session.php
> >>>>
> >>>> The big pink box has more info.
> >>>>
> >>>> --
> >>>> Postgresql & php tutorials
> >>>> http://www.designmagick.com/
> >>>>
> >>>
> >>>
> >>>
> >>> --
> >>> PHP Database Mailing List (http://www.php.net/)
> >>> To unsubscribe, visit: http://www.php.net/unsub.php
> >>>
> >>>
> >>
> >
> >
> >
> > --
> > PHP Database Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Confidentiality Notice:
This e-mail and any attachments may contain confidential information
intended solely for the use of the addressee. If the reader of this message
is not the intended recipient, any distribution, copying, or use of this
e-mail or its attachments is prohibited. If you received this message in
error, please notify the sender immediately by e-mail and delete this
message and any copies. Thank you.
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: losing MySQL resource
am 12.11.2009 22:51:22 von dmagick
Stan wrote:
> I don't understand the process. When session_start() reconnects to a (the)
> session, objects saved as $_SESSION variables must be "recreated". To do
> this requires the class definition. So the class definition must be loaded
> prior to calling session_start().
>
>
>
> Some other languages I've written in had a process called serialization
> through which objects were saved. And an opposite (deserialization?)
> process through which objects were restored. To have "persistent" objects
> requires some way to "save" the objects for the next invocation of the
> application (PHP in this case). Deserialization necessarily requires the
> template upon which the original object was built (the class definition).
>
>
>
> I found http://us.php.net/serialize in the online PHP manual. I also found
> http://us.php.net/manual/en/language.oop5.autoload.php in the online PHP
> manual (I have not tried autoload).
>
>
>
> I simply moved the require_once("class_definitions.inc"); to appear
> immediately before my session_start(); and everything worked thereafter.
When data gets serialized in the session, it only stores the data itself
(public/private variables). It does not store the actual class (your
methods) and the code that makes it all work. That would just bloat the
session and cause other extra headaches.
So before the data can be properly loaded, you need the class.
http://us.php.net/manual/en/language.oop5.serialization.php
explains things a little better I hope.
--
Postgresql & php tutorials
http://www.designmagick.com/
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: losing MySQL resource
am 19.12.2009 15:22:54 von Stan
OK. I finally understand. When I want to save an object across client
requests, I need to serialize() that object into a session variable and when
I need to use it later I need to unserialize() it into a local variable. If
I modify the object, I need to replace the serialized session variable.
I've tested this.
Or I can let PHP do it ... and that appears to work ... as long as I load
the class definitions prior to session_start() ... which appears to conflict
with the manual's instruction of using session_start(). And I've tested
this.
Or I can use an __autoload callback? I don't see where I need to place the
__autoload function itself ... and how do I tell PHP to use it ... or is it
completely automatic?
Thanks,
Stan
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php