Martn Posted April 11, 2021 Share Posted April 11, 2021 I have 2 PHP scripts : opendb.php and closedb.php In opendb.php I have this code : <?php session_start(); $conn = mysqli_connect(................................); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } else { echo "DB open."; $_SESSION['dbhandler'] = $conn; } ?> In closedb.php I have this code : <?php session_start();$conn = $_SESSION['dbhandler'] mysqli_close($conn); echo "DB closed"; ?> When I run opendb.php I get the expexted message "DB Open." But when running the closedb.php , I get an error 500. I was hoping to be able to pass the Handle, needed to operate in my database, between PHP scripts, by pushing it into $_SESSION['dbhandler'] , and retreiving it in other scripts. Is this possible ? Regards, Martin Quote Link to comment https://forums.phpfreaks.com/topic/312455-pass-handle-between-php-scripts/ Share on other sites More sharing options...
mac_gyver Posted April 11, 2021 Share Posted April 11, 2021 database connections are resources. all resources on a web page are destroyed by php when the php script ends, i.e. you cannot pass a database connection in a session variable. you must make a new database connection on any page that needs one. as to the http 500 error, you have a php syntax error in the code on that page. find the php.ini that php is using and set error_reporting to E_ALL and display_errors to ON, so that php will help you by reporting and displaying ALL the errors it detects. Quote Link to comment https://forums.phpfreaks.com/topic/312455-pass-handle-between-php-scripts/#findComment-1585723 Share on other sites More sharing options...
MadTechie Posted April 11, 2021 Share Posted April 11, 2021 (edited) See mac_gyver post and for the 500 error, its probablty the missing ; <?php session_start(); $conn = $_SESSION['dbhandler'] //<--missing ; mysqli_close($conn); echo "DB closed"; ?> Instead of passing parameters for the connection, just include the details <?php //db_config.php defined('DB_HOST') or define('DB_HOST', 'localhost'); defined('DB_USERNAME') or define('DB_USERNAME', 'username'); defined('DB_PASSWORD') or define('DB_PASSWORD', 'password'); <?php //database.php require(__DIR__.'/db_config.php'); // Create connection $db_conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?> (code above is not tested and is just as an example) you could build classes from this etc Hope this helps Edited April 11, 2021 by MadTechie Quote Link to comment https://forums.phpfreaks.com/topic/312455-pass-handle-between-php-scripts/#findComment-1585734 Share on other sites More sharing options...
MadTechie Posted April 11, 2021 Share Posted April 11, 2021 I should also point out instead of building a class you could include the database.php and just use the $db_conn variable.. Depending on your end goal Quote Link to comment https://forums.phpfreaks.com/topic/312455-pass-handle-between-php-scripts/#findComment-1585743 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.