Solved

Connect to mySQL db ONLY if not already connected

Posted on 2007-11-28
2
177 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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now