Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Connect to mySQL db ONLY if not already connected

Hello,

I want to connect to a mySQL database but ONLY if I am not already connected to a database.

             mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable toconnect to database");
             mysql_select_db("$DBName") or die("Doh!");

So if my script is called as an include from a script that has already established a connection with the DB then it should not connect again.
0
hankknight
Asked:
hankknight
1 Solution
 
nplibCommented:
if (!$conn) {
$conn =  mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable toconnect to database");
             mysql_select_db("$DBName") or die("Doh!");
}
0
 
steelseth12Commented:
Have a look at the Singleton pattern

http://www.php.net/manual/en/language.oop5.patterns.php
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now