Docs about temp tables say that when the connection and the session is closed the local temporary table are dropped automatically.
But doesn't PHP work this way? Any variable I want to persist ( for a single user) I need to keep in a session variable. When does a session close in PHP? Is there a specific statement I need to avoid so I don't drop my temp table?
CodePudding user response:
There is no link between a "database session" and a "PHP session", and no way to make such a link.
The way a "database session" works is this:
- The user's browser sends your server an HTTP request
- Your HTTP server starts up a new PHP process or thread to handle the request
- That PHP process or thread creates a new connection to the database server (a "database session")
- You execute some SQL, which can make use of temporary tables within that connection
- When you've done what you need to do for this request, the PHP process or thread is cleaned up
- If you hadn't already closed it, your database connection is closed here
- If you hadn't already dropped all temporary tables in the connection, they will be dropped now
Meanwhile, a "PHP session" works like this:
- The user's browser sends your server an HTTP request
- Your HTTP server starts up a new PHP process or thread to handle the request
- That PHP process or thread reads a cookie value from the request, and looks for a session with that ID in some store (by default, a file)
- You can then read and write the data associated with that session ID
- When you've done what you need to do for this request, the PHP process or thread is cleaned up
Note that at no point do you choose which database connection to use, or have any way of "storing" it between requests.
If you want data to persist between requests, you need to store it somewhere other than a temporary database table - a permanent database table, or a PHP variable serialized into the PHP session.