Jump to content

pass handle between PHP scripts


Martn

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by MadTechie
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.