Re: [PHP-DB] losing MySQL resource
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-f51080621...@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: [PHP-DB] losing MySQL resource
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-f51080621...@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: [PHP-DB] losing MySQL resource
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: [PHP-DB] losing MySQL resource
How do I make an Object persistant for the duration of a Session? Thanks, "Chris" wrote in message news:4ab6b16...@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