简体   繁体   中英

Can i use sql server temporary tables in PHP?

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?

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:

  1. The user requests a page - types in a URL, clicks a link, submits a form, etc
  2. The user's browser sends your server an HTTP request
  3. Your HTTP server starts up a new PHP process or thread to handle the request
  4. That PHP process or thread creates a new connection to the database server (a "database session")
  5. You execute some SQL, which can make use of temporary tables within that connection
  6. When you've done what you need to do for this request, the PHP process or thread is cleaned up
  7. If you hadn't already closed it, your database connection is closed here
  8. If you hadn't already dropped all temporary tables in the connection, they will be dropped now

Meanwhile, a "PHP session" works like this:

  1. The user's browser sends your server an HTTP request
  2. Your HTTP server starts up a new PHP process or thread to handle the request
  3. 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)
  4. You can then read and write the data associated with that session ID
  5. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM