Forum Moderators: open

Message Too Old, No Replies

Open Connection/Close Connection in MySQL

How often on a various web page with multiple queries?

         

jimh009

4:17 am on Feb 7, 2006 (gmt 0)

10+ Year Member



Hi,

I'm new to PHP and MySQL so this is probably an easy question. But how often should I open/close the connection on a web page.

For example, let's say on a particular web page I run 3 database queries. Should I open/close the database connection for EACH query. Or should I have a open connection at the begnning of the first query and a close connection at the end of the last query?

Thanks.

Jim

txbakers

2:19 pm on Feb 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would open the connection once and close it once.

mattglet

4:06 pm on Feb 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would open once right before you first need it, then close it immediately after the last place you use it. Just make sure you also close it where necessary in between (if you redirect away from the page, or stop the page during error checking, etc.).

The reason you don't want to do it a bunch of times is because it taxes resources opening/closing the object numerous times.