I have a situation in my project where I have to keep using a lot of SQL queries. I have two questions here, one is, once a create a connection with a database, does it remain persistent throughout all my web pages ? i.e., my client remains connected with the mySQL server throughout. If yes, then is it a safe option to save my mySQL link identifier as a session variable i.e.,
$_SESSION['link'] = $link //( $link is mysql link identifier )
and therefore use this variable throughout my web pages, thereby only having one connection to the database. I cannot use
mysql_pconnect().
My second question is, is it fine to keep keep connecting and disconnecting from the database time and again. Is it a a good practice ? I am using mySQL server but have only limited access to it.
My opinion is, that you do not save nor win anything when you try store your
$linkto session.You have to make connection to sql server for each page data load. Is the same as if you include file. If this variable is stored in session or not brings a same efect – it’s same cost of memory in the end count.
Must now – after you load the page the show is over 🙂 connection are lost, only content of your sessions left. ((Yes, it depends on your webhost settings if it is closing db connection automatically or in some time period or is waiting till you will do it with command. The last is the best for saving resources))
You should start thinking about stotring the loaded contnet to session or file. Then you can save connections and data loads from external resources, what was maybe your base idea.