LemonInflux Posted February 23, 2008 Share Posted February 23, 2008 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 Quote Link to comment Share on other sites More sharing options...
mainewoods Posted February 26, 2008 Share Posted February 26, 2008 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 26, 2008 Share Posted February 26, 2008 If you happen to be on a server configuration where persistent connections work and are permitted by the hosting company, read this - http://php.net/mysql_pconnect Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.