Jump to content

Single database connection?


LemonInflux

Recommended Posts

This is more PHP than AJAX, but I don't think PHP will help.

 

I have a code that retrieves output from a php file which connects to MySQL, which is constant (setInterval).

 

On my php file, I have a database connection. I suddenly realised, this means every time the function runs, it re-connects to the database.

I'm supposing this takes up time I do not need to spend?

 

When I tried to include the database file on the page with the ajax, the php that got the database information couldn't connect to the database (it said there wasn't a connection). Why is this? Surely there must be a way to connect to a database before the function, so the function doesn't need to re-connect over and over again?

 

Thanks in advance,

 

Tom

Link to comment
Share on other sites

every time ajax calls a php page, that php page has to reconnect to the db if it wants to use the db.  This is caused by the stateless nature of the web. By putting the php db connect code in the page with the ajax, the db connection would be executed when the page was executing on the server, but the db connect would be closed automatically at the end of the execution of that page.  It closes automatically at the end of the script execution. By the time you're able to execute the ajax browser side, the db connection is already closed.  There is no way around this, it is caused by the stateless nature of the web.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.