?
Solved

Connect to mySQL db ONLY if not already connected

Posted on 2007-11-28
2
Medium Priority
?
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

801 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