[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Connect to mySQL db ONLY if not already connected

Posted on 2007-11-28
2
Medium Priority
?
194 Views
Last Modified: 2013-12-12
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
Comment
Question by:hankknight
2 Comments
 
LVL 17

Accepted Solution

by:
nplib earned 2000 total points
ID: 20367382
if (!$conn) {
$conn =  mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable toconnect to database");
             mysql_select_db("$DBName") or die("Doh!");
}
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20367469
Have a look at the Singleton pattern

http://www.php.net/manual/en/language.oop5.patterns.php
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month20 days, 10 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question