0

I create a MySQL connection object using mysqli_connect. The object works to access the database (e.g. mysqli_query). Using print_r I can see that it contains information I passed as parameters to mysqli_connect (e.g. host_info). However, when I save it in a $_SESSION variable, and then retrieve it later, it is mostly blank, and contains none of the data I initialized it with. I use pretty much the same method with FileMaker which also returns an object with the first command, and have no problem retrieving and re-using the object.

My workaround is to simply keep calling mysqli_connect to generate a new object (which works just fine), but that doesn't seem ideal.

Is there something special about this object that makes it impossible to be saved in a $_SESSION variable?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
David Crowe
  • 113
  • 6
  • 1
    Does this answer your question? [Storing database connection in a session variable](https://stackoverflow.com/questions/6078843/storing-database-connection-in-a-session-variable) – jmalenfant Dec 29 '19 at 05:02

1 Answers1

2

It is better to make a config file for connection to database and use it on everywhere you need to that connection, store it in a SESSION using your memory and some time maybe Session become destroy and your application become crash. but is you want to store an object or array in a Session you must use serialize. See the below example:

$_SESSION['connection'] = serialize($connection);

$connection= unserialize($_SESSION['connection']);

When want to store it in a session use serialize function, and when want to use it first unserialize then use it

bahram
  • 92
  • 8