2

I created a temporary table in one php file, and want to access it in another php file. the scripts run sequentially. I used mysqli and am prepending p: to hostname.

The problem is in my second php file, I cant access my temporary table. So I wanted to know if its possible to do this, or not? And if yes how? Am using WAMP server.

No name
  • 81
  • 1
  • 13
  • 1
    Show us what you tried. **Reminder**: We are not here to create code for you. – Nytrix Jul 22 '16 at 14:48
  • 1
    Judging by your requirement, what you need is not a temporary table but an actual, real table. In that case, your problem simply disappears since both scripts will be able to access the same data source. – Mjh Jul 22 '16 at 15:26
  • @Mjh, I wanted to use the temporary table because when In the second script i read data from it, and after drop it. I think in terms of perfomance it might be better creating the temporary table, but i could be wrong. – No name Jul 22 '16 at 17:09

2 Answers2

0

Not possible, directly. Temporary tables are destroyed when the connection used to establish them is closed. When your "create" script shuts down, its DB connection is closed, and mysql cleans - including destroying that temp table.

That means when your "use" script fires up, it gets a new connection, without any of the stuff that the first script did.

There are persistent connections available in PHP, but those connections exist in a pool, and there is no control over WHICH connection any particular scripts gets from that pool. You may get lucky and receive the same connection for two different scripts, but it's purely by chance.

You'd need some OTHER 3rd script that operates continously to hold open the mysql connection, leaving the temp table in place. And your other two scripts would communicate with this third one.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 1
    In `WAMP`'s case, what you wrote might be true, but I conducted a test. I use `php-fpm` with `pm.start_servers = 2` and `pm.max_requests = 0` so that the process doesn't exit, for purposes of this test. I connected to MySQL by creating a persistent PDO connection. In MySQL's terminal I verify that there's an established connection by issuing `SHOW PROCESSLIST\G;` which shows 1 connection with an id of 6. I "hammer" my php endpoint, which performs a select query. After 500 http requests, issuing `SHOW PROCESSLIST\G;` shows one and the same active connection. – Mjh Jul 22 '16 at 14:57
  • if you only ever have ONE user of the site, and NEVER have 2+ parallel requests on the site, then you may well only ever have 1 pcon in the pool. But as soon as you get 2+ scripts running in parallel, they'll establish TWO connections, and now all bets are off. – Marc B Jul 22 '16 at 14:58
  • 1
    Using non-persistent connections and issuing `SHOW PROCESSLIST\G;` shows a different connection process. Given the fact that I might be wrong, I'd like to see what other people think about this, since I know that doing hasty tests definitely leads to mistakes somewhere :) – Mjh Jul 22 '16 at 14:59
  • 1
    I'm using nginx and `wrk` to simulate connections. If it's one user or two users, the connections are multiplexed by nginx so does it really matter? This is the beauty of fastcgi apps. – Mjh Jul 22 '16 at 15:00
  • thanks for the insight on this question, its a good explication about this. – No name Jul 22 '16 at 17:14
0

From http://php.net/manual/en/mysqli.persistconns.php

The persistent connection of the mysqli extension however provides built-in cleanup handling code. The cleanup carried out by mysqli includes:

(worth reading the other things too but the important bit is)

  • Close and drop temporary tables

In short, a temporary table is just that, temporary. It's not meant to be used for other purposes than to temporarily store some data for one specific operation. If you want a more permanent thing consider using a concrete table with a memory storage engine.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • Yes I have read that, but still thought it would be possible. concrete table with a memory storage engine means? – No name Jul 22 '16 at 17:06