Connecting Tech Pros Worldwide Help | Site Map

How to access a temporary table created in page1 from page2?

Newbie
 
Join Date: May 2007
Posts: 13
#1: Aug 22 '07
Hi!

Here's my situation:

I created a temporary table TEMP1 in PAGE1.PHP and inserted a few rows. Before I left PAGE1.PHP i tried "SELECT * FROM TEMP1" and echoed the rows and surely there they were.

Then I jumped to PAGE2.PHP using header("Location: PAGE2.PHP") and tried "SELECT * FROM TEMP1" from there and could not echo anymore the rows.

Can anyone pls help me and show me how to access a TEMPORARY TABLE created on one page and accessed its contents (rows) from another page?

A short and simple example will be greatly appreciated.

Learning
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: Aug 22 '07

re: How to access a temporary table created in page1 from page2?


Heya, Learning.

Temporary tables are deleted once you close the MySQL connection (which is done automatically at the end of script execution).

You have a couple of options.
  • You can save the data as an array in the _SESSION until you're ready to commit it to your database. This is probably your best option, as it is the least likely to blow up in your face.
  • You can use MySQL persistent connections. This would preserve your temporary tables because PHP would not close the MySQL connection at the end of the script's execution (more or less), BUT it then becomes up to you to manage your MySQL connections! MySQL will auto-close persistent connections after a certain amount of inactivity, so it's not a huge problem. But I would generally recommend against using persistent connections, as they are usually not necessary and encourage bad habits.
Newbie
 
Join Date: May 2007
Posts: 13
#3: Aug 23 '07

re: How to access a temporary table created in page1 from page2?


Quote:

Originally Posted by pbmods

Heya, Learning.

Temporary tables are deleted once you close the MySQL connection (which is done automatically at the end of script execution).

You have a couple of options.

  • You can save the data as an array in the _SESSION until you're ready to commit it to your database. This is probably your best option, as it is the least likely to blow up in your face.
  • You can use MySQL persistent connections. This would preserve your temporary tables because PHP would not close the MySQL connection at the end of the script's execution (more or less), BUT it then becomes up to you to manage your MySQL connections! MySQL will auto-close persistent connections after a certain amount of inactivity, so it's not a huge problem. But I would generally recommend against using persistent connections, as they are usually not necessary and encourage bad habits.

Hi Pbmods,

Thank you for your prompt response to my problem.

I'll try your suggestion right now. But for my better understanding, what do you mean by "at the end of the script execution"? Does this mean at the end of every web page? Does this mean that the MYSQL connection automatically closes everytime it leaves a webpage?

Thanks again. I really appreciate it.

Learning
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#4: Aug 23 '07

re: How to access a temporary table created in page1 from page2?


Heya, Learning.

You got it.

When the browser requests a page from your server, the server checks the request and determines (usually by the file extension) what to do with it. So for a .php file, the server will launch the PHP executable and feed the .php file into it.

PHP will then parse the .php script and execute it, including opening a connection to the MySQL database server and playing around with the database.

Once PHP reaches the end of the script, it begins cleaning up and preparing to terminate. It releases the memory it allocated to store variables, closes any files that it opened and it closes its connection to the MySQL database server.

Once it's finished doing all that, the PHP executable itself exits.

Then the server finishes sending the HTML to the browser and then it goes dormant and waits for the next HTTP request.
Newbie
 
Join Date: May 2007
Posts: 13
#5: Aug 23 '07

re: How to access a temporary table created in page1 from page2?


Quote:

Originally Posted by pbmods

Heya, Learning.

You got it.

When the browser requests a page from your server, the server checks the request and determines (usually by the file extension) what to do with it. So for a .php file, the server will launch the PHP executable and feed the .php file into it.

PHP will then parse the .php script and execute it, including opening a connection to the MySQL database server and playing around with the database.

Once PHP reaches the end of the script, it begins cleaning up and preparing to terminate. It releases the memory it allocated to store variables, closes any files that it opened and it closes its connection to the MySQL database server.

Once it's finished doing all that, the PHP executable itself exits.

Then the server finishes sending the HTML to the browser and then it goes dormant and waits for the next HTTP request.


Hi again Pbmods,

I've tried your least recommended option cause it was the easiest to try out. And it did work. I'm still finishing the _SESSION array but I'm just wondering,... how do you close a persistent connection?

Again,
Learning
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#6: Aug 23 '07

re: How to access a temporary table created in page1 from page2?


Heya, Learning.

Actually, it looks like I have some learning to do, too :P

Using mysql_pconnect() uses a single persistent connection that gets used by the Apache child process.

After a certain number of connections, Apache will terminate and respawn its child processes, which means that the behavior of your script may be a tad unpredictable.

As a result, there is no way to close a persistent connection.

Summer Reading Guide:
Persistent Connections Guide (PHP Manual)
mysql_pconnect() (PHP Manual)
Can I close persistent connections? (FAQTs)
Newbie
 
Join Date: May 2007
Posts: 13
#7: Aug 24 '07

re: How to access a temporary table created in page1 from page2?


Quote:

Originally Posted by pbmods

Heya, Learning.

Actually, it looks like I have some learning to do, too :P

Using mysql_pconnect() uses a single persistent connection that gets used by the Apache child process.

After a certain number of connections, Apache will terminate and respawn its child processes, which means that the behavior of your script may be a tad unpredictable.

As a result, there is no way to close a persistent connection.

Summer Reading Guide:
Persistent Connections Guide (PHP Manual)
mysql_pconnect() (PHP Manual)
Can I close persistent connections? (FAQTs)

Thank you so much Pbmods for your time. I'm learning more and more... and I also appreciate the reading guide links. They will be very helpful.

Thanks again.

Learning
Reply