arianhojat Posted September 11, 2007 Share Posted September 11, 2007 I want to store database connection in session so dont need to reconnect to database on each page but wasnt sure what is best way to do this. My 1st impluse is in my php/session code, is to do something like this: <?php //On session page session_start(); //needed require_once 'MDB2.php'; if(!$_SESSION['uid'])//session not set, get users info and set it in session { $db_host = 'localhost'; $db_user = 'root'; $db_pass = 'password'; $db_name = 'mysql'; $dsn = "mysql://$db_user:$db_pass@unix+$db_host/$db_name"; $mdb2 =& MDB2::connect($dsn); if( !(PEAR::isError($mdb2) )//test a query to generic mysql database just to test if connected { $_SESSION['dbAbstract'] = serialize( $dbAbstract ); } $_SESSION['uid'] = $_SERVER["AUTH_USER"]; } //On php page require_once 'MDB2.php'; include 'session.php'; $dbAbstract = unserialize( $_SESSION['dbAbstract'] ); $testsql = 'SELECT * FROM mysql.help_category'; $result =& $mdb2->query($testsql); if( PEAR::isError($result) ) { echo "<br />Search error..."; die($result->getMessage()); } ?> But what if loses connection, even though session is still alive. Link to comment https://forums.phpfreaks.com/topic/68835-best-way-to-store-database-w-abstraction-layer-like-mdb2-in-session-code/ Share on other sites More sharing options...
Jessica Posted September 11, 2007 Share Posted September 11, 2007 You don't need to store a database connection in the session. Have a config file in which you connect using mysql_pconnect, and include that file on all of your pages. Link to comment https://forums.phpfreaks.com/topic/68835-best-way-to-store-database-w-abstraction-layer-like-mdb2-in-session-code/#findComment-345995 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.