Viewed   61 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.

Saturday, December 3, 2022


  1. You are updating in tblCount EACH time, because your session is closed each time your script finishes. SO: Put the session_start()call as the FIRST LINE in code.
  2. It's not permitted to set an integer as $_SESSION variable. So if you set $_SESSION[$pagenumber] = 'something', then you gain the following notice:

( ! ) Notice: Unknown: Skipping numeric key 1 in Unknown on line 0

Quite... not understandable. For details see this answer.


Add your $pagenumber as index in an array (here pagenumbers) and that array inside the $_SESSION variable. No notice anymore.


$pagenumber = 1;

if (!isset($_SESSION['pagenumbers'])) {
    $_SESSION['pagenumbers'] = array();

if (!isset($_SESSION['pagenumbers'][$pagenumber])) {
    $_SESSION['pagenumbers'][$pagenumber] = $pagenumber;

echo 'Page number: ' . $_SESSION['pagenumbers'][$pagenumber] . '<br/>';
$views = getViews($pagenumber);
echo '<pre>Viewed ' . print_r($views, true) . ' times</pre>';

Note: I used my functions to test. They just replace your db-processing code.

Tuesday, August 2, 2022

Store the actual array in session:


$query = //my query. 
$result = array();
$res = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($res, MYSQL_NUM){
    $result[] = $row;
$_SESSION['query2'] = $result;

//I have already done session_start(); and initialized $conn

$tempArray =  $_SESSION['query2'];
Sunday, December 11, 2022

Within your content.php use require(db_conn.php); And if you have a function defined in there, you're going to have to put require(db_conn.php); INSIDE that function.

The database connection doesn't seem to have exactly the same scoping as a standard variable.

I know when coding it, you feel like "I should only have to include this once!" but shrug get everything else working first, and if someone wants to pay you to go back and try to "fix" this... work on it then. ;)

Friday, November 25, 2022

Your DBConnection class would need an additional method:

public function getLink()
    return $this->mysqli;

It seems that your original User class was a subclass of DBConnection, because mysqli property on DBConnection is protected and User class has a parent::__construct() call.

It's better to use dependency injection, so your User class will receive its database connection via the constructor:

public function __construct(DBConnection $db)
    $this->mysqli = $db->getLink();

Then from your code you can run:

$db = new DBConnection;
$uObj = new User($db);
Thursday, September 22, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :