Passing a MySQL connection to another script

I’m developing a script that connects to a MySQL database, which for the first invocation of the script is successful, however I need to re-call the script for another database query. On subsequent invocations of the script the database connection is lost.

Is there a PHP command or method for maintaining the link throught the script process, no matter how many times the script calls itself? I’ve looked at persistent connections and can’t see the methodology for doing so.


the simplest and most secure way is to create a seperate file with the connection details in it, then just do a require_once or include_once on it (i typically use require_once since the pages won’t fill out properly without it).

The connection details: would that be the login information?

In the meantime, I’ll look up ‘require_once’ and see what that does.

You say that’s the simplest way, and while that works for me, is there also a better way?


Its only 2 lines of code, not really a better way of doing it.

Is there an example of this somewhere? I’m a complete beginner at this.

mySQL_connect(host, user, password)

It seems such a simple solution that I wonder if I’m missing something…

I’ll experiment with it. Thanks!

Sponsor our Newsletter | Privacy Policy | Terms of Service