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?

 Answers

80

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
 
nomie
answered 7 Months ago
54

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');
  mysql_select_db('db');
  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
 
altermativ
answered 7 Months ago
62

when your login is success on login page.

session_start();
$_SESSION['user_logged_in'] = true;

in your logout page

session_start();
unset($_SESSION['user_logged_in']);   
session_destroy();

in your home page

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

You need to unset the session vars. See http://php.net/manual/de/function.session-unset.php

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

Saturday, May 29, 2021
 
sholsinger
answered 5 Months ago
39

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/config.inc.php

Tuesday, July 13, 2021
 
ShadowZzz
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 :