Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Connect to mySQL db ONLY if not already connected

Posted on 2007-11-28
2
Medium Priority
?
192 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
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.
Suggested Courses

609 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