Two calls to PHP/MySQL function results in No Database found error


I have a function I am writing. This is just to help me be able to dynamically put content on a webpage. Perhaps I am proceeding about this the incorrect way but I am running into an error and am wondering if anyone can point me in the correct direction or help me resolve this issue.

Issue: When I call the same function twice I receive a No Database Found error.

Notes: When I simply call the function 1 time there is no issue. It works perfectly. So if I remove the dbcon(‘indexpara1’); then the title shows up no problem or if I remove the dbcon(‘title’); then the function works no problem to display the paragraph text.

I asked this in another forum and I was told to separate the database connect portion of the function and use an include at the top of every page. I asked how to do that without ruining the integrity of the function since that whole function is database connect and have not heard back.

Request: I would like to know how to properly call that function more than once to be able to display multiple items throughout the webpage such as title, meta tags, first paragraph text, second paragraph text, etc etc. Is there a better way of performing the task I am attempting to perform such as making a class or something like that?

Current website address: (don’t forget to put the http:// I do not wish it to be a link. I would rather the search engines not pick it up since it is only temporarily there.)

Thank you for any help you can provide.

Code for webpage
Please note that I have removed a bunch of the div’s, text and paragraphs that do not pertain to my question. I have also utilized psuedo information in place of the private information below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"



<script type="text/javascript"> 
function suppress_errors() {return true;}
<?php dbcon("indexpara1"); ?>
<p>This is some text that did not come from the database.  This is typed directly into the website.</p>

dbcon function:

$user_name = "username";
$password = "password";
$database = "database";
$server = "localhost";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

//																					 //
// The dbcon function is made to make SEO a little easier and requires a database    //
// to be set up. The database that is used in the making of this script is MySQL 5.1 //
//																					 //
// The script is written with uniformity in mind.  The database rows, case statement //
// and parameter sent to the function should all be the same name.					 //
//																					 //

// Begin the database connection function (ie. dbcon)
// The dbcon function accepts 1 parameter.  This is required when you call the function.
// This parameter name should be the same name as your case statement and the row name in the database table.
function dbcon($data) {
	global $user_name, $password, $database, $server, $db_handle, $db_found;
	// We are using a switch case statement to decipher which parameter you sent through the function.
	switch($data) {
		case "title":
			$codeop = "<".$data.">";
			$codecl = "</".$data.">";
			$default = "<title>This is the default title if no title is found in the database or we cannot make a connection</title>"; // Enter a default title here in case we cannot make a connection to the database.
			$db_table = 'table1';
		// You can add as many cases as you would like or remove the ones you do not want.  In the example below there are 4 paragraphs on the index page.
		case "indexpara1":
		case "indexpara2":
		case "indexpara3":
		case "indexpara4":
			$codeop = "<p>";
			$codecl = "</p>";
			$default = "This is the default text if there is no text for the index page paragraph found or if we cannot make a database connection.";
			$db_table = 'table2';
			echo "The website cannot find information for" . $data . "Please contact the website administrator or webmaster about this error.";

	// Now we start our database connection.   The ""'s below must stay there.  You will only need to change the following below:
	if ($db_found) {
		$open = @mysql_connect($server,$user_name,$password);
		if (!$open) {
			// If we found a database but cannot open the database then use the default setting and echo a statement.
			echo $default;
			echo("Website Error: The website cannot connect to database!  Please contact the website administrator about this error.");
		elseif ($open) {
			$SQL = mysql_query("SELECT ". $data ." FROM " . $db_table) or die(mysql_error());
			$dbinfo = mysql_fetch_array($SQL);
			if(!empty($dbinfo[$data])) {
				echo $codeop . stripslashes($dbinfo[$data]) . $codecl;
			// If someone enters a blank entry into the submission form and it is not caught by JavaScript verification the utilize the default.
				echo $default;
			// Close the database connection.
	else {
		// if we cannot find a database then we echo the default and close the database.
		echo $default;


You generally don’t need to create more than one connection per page, nor would you want to. Imagine if you had hundreds of users hitting hundreds of pages per minute, each making several connections per page. Pretty soon you have thousands of database connections running at once. Not necessary or desirable.

Generally, you only need to create the connection once per page, if that. Lots of developers destroy connections at the end of a page or function. Some go to the other extreme and set up a single, persistent connection that carries over for the entire site. You can read up on the pros and cons and decide what is best for you, but you don’t need concurrent connections running in a single script.

You might try separating out the database connection part, including it once at the beginning of the pages that need it, and then simply pass the $db_handle around to your functions. For that matter, you might try NOT passing the $db_handle around and see what happens. You might be surprised to find that PHP doesn’t always need you to be explicit and will grab whatever connection it finds already open.

Sponsor our Newsletter | Privacy Policy | Terms of Service