Welcome to WebmasterWorld Guest from 54.167.83.224

Forum Moderators: open

Message Too Old, No Replies

Ajax, PHP and MySQL. Why isn't db open?

     
1:33 am on Sep 6, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:June 12, 2004
posts: 119
votes: 0


After struggling with Ajax for a few years, I hunkered down and finally got into it. Not as difficult as it first seemed.

I do have one question regarding opening and closing the MySQL database.

Main Program
openMysqlDbFunction;
do some assorted stuff
Do the Ajax thing
do some db stuff
close database


PHP called by Ajax

do some db stuff


This will give me an error because the database is not opened.

PHP called by Ajax

openMysqlDbFunction;
do some db stuff


1) Why do I have to open it again even though it's open in the main calling program?
2) Because I'm opening it again in the Ajax PHP code, does that close the DB in the main program?
3) Suppose the Ajax routine is called several times in the Main Program. Am I not doing things cleanly? Should I close the DB in the Ajax/PHP code?

Thanks for the help.
8:17 am on Sept 6, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


I initially wrote a reply regarding ajax/db but then wondered if you are referring to persistent database connections? (Although this is the JavaScript forum). Normal db connections are not persistent between requests. However, if you are querying the ajax/request process then persistent connections are probably best avoided and probably not required.
4:35 pm on Sept 6, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


Why do I have to open it again even though it's open in the main calling program?


If I get you right, you mean "the page containing the ajax" when you say "main program." So this (PHP?) page opens a connection, outputs the data, closes connection. Your Ajax request, then, is like a separate "page" being called, it has to re-open a new connection to do "whatever." And when it's done, it closes again.