Asked  7 Months ago    Answers:  5   Viewed   26 times

I'm trying to pass a session with mysqli connection for multiple queries on the site, but when I try to do a query it outputs a warning "Couldn't fetch mysqli"

$_SESSION['db']=new mysqli($host,$username,$password,$db);

Is it impossible to pass a mysqli connection reference thru session? is there a different method to use?



Yes, it is explicitly impossible.

See PHP Documentation here mentioning in a highlighted Warning: "Some types of data can not be serialized thus stored in sessions. It includes resource variables or objects with circular references (i.e. objects which passes a reference to itself to another object)."

MySQL connections are one such kind of resource.

You have to reconnect on each page run.

This is not as bad as it sounds if you can rely on connection pooling via mysql_pconnect(), but first see more background info on mysql_pconnect() in this article.

Wednesday, March 31, 2021
answered 7 Months ago

A PHP resource is a special type that already is a reference in itself. Passing it by value or explicitly by reference won't make a difference (ie, it's still a reference). You can check this for yourself under PHP4:

function get_connection() {
  $test = mysql_connect('localhost', 'user', 'password');
  return $test;

$conn1 = get_connection();
$conn2 = get_connection(); // "copied" resource under PHP4

$query = "INSERT INTO test_table (id, field) VALUES ('', 'test')";
mysql_query($query, $conn1);
print mysql_insert_id($conn1)."<br />"; // prints 1

mysql_query($query, $conn2);
print mysql_insert_id($conn2)."<br />"; // prints 2

print mysql_insert_id($conn1); // prints 2, would print 1 if this was not a reference
Wednesday, March 31, 2021
answered 7 Months ago

when your login is success on login page.

$_SESSION['user_logged_in'] = true;

in your logout page


in your home page

   if(isset($_SESSION['user_logged_in'])) {
   < a href='logout.php'>Logout</a>
else {
    < a href='login.php'>Login</a>
Wednesday, March 31, 2021
answered 7 Months ago

You need to unset the session vars. See

Means, put session_unset() before you destroy the session.

Saturday, May 29, 2021
answered 5 Months ago

yo need create the user "pma" in mysql or change this lines(user and password for mysql):

/* User for advanced features */
$cfg['Servers'][$i]['controluser'] = 'pma'; 
$cfg['Servers'][$i]['controlpass'] = '';

Linux: /etc/phpmyadmin/

Tuesday, July 13, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :