Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL + PHP: Cant connect to database twice?

Status
Not open for further replies.

evBenji

Programmer
Dec 17, 2010
2
AU
Hey i have a little script that list headlines.

The SQL code is inside of a php file and i made it into a php class to try and get it working. it looks as follows...

<?
class headlineClass{

function listHeadlines($tpath, $c, $lim) {
// Automatically get $tpath to avoid possible security holes
$tpath = realpath(__FILE__);
$tpath = substr($tpath,0,strrpos($tpath,DIRECTORY…
// Check if the file exists on local server and include it
if(file_exists($tpath . "cn_config.php")) {
require_once($tpath . "cn_config.php");
} else {
die("Could not include required configuration file");
}
// Check if a connection to the database was established
if(!isset($link)) {
die("Please make sure the \"\$tpath\" variable is the root path to where 'headlines.php' is on your server.");
}
// Set limit for number of items displayed
if(!isset($lim)) { $lim = "5"; }

$result = mysql_query("SELECT * FROM $t_news WHERE cat = '$c' ORDER BY date DESC LIMIT 0, $lim", $link);
echo "<ul>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// HTML OUTPUT................
echo "<li>" . $row['subject'] . "</li>";

}
echo "</ul>";
mysql_free_result($result);
mysql_close($link);
}

}
?>

So on a seperate file where i want the headlines displayed a make a class and the headlines are listed. The call is as follows...

<?php include("path/allHeadlines.php"); ?>
<?php $testClass = new headlineClass(); $testClass->listHeadlines("path/",8, 1 ); ?>

But when i want to do make another class thats exactly the same by doing this...

<?php $testClass1 = new headlineClass(); $testClass1->listHeadlines("path/",8, 1 ); ?>
<?php $testClass2 = new headlineClass(); $testClass2->listHeadlines("path/",8, 1 ); ?>

The first call always works, but the second time round it seems my path is blocked and i cant get back to the database and list the items again?

Can anyone see why? The errors comes at if(!isset($link)) { and the echos

Please make sure the \"\$tpath\" variable is the root path to where 'headlines.php' is on your server.

 
This is a PHP question more than it is a MYSQL as such it would better be posted in forum434.

However, to address your issue: your class is actually closing your own database connection.

Code:
mysql_free_result($result);
[red]mysql_close($link);[/red]

However your class doesn't open it, so when you try to call it a second time, the DB connection is closed, and as such your $link variable is no longer set.

Simple solution is to not close the actual DB connection, or make your class create the DB connection every time.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Hi thanks for reply. Yeah i guess if i put in php forum they would have said put it here.

Um i tried to remove the mysql_close($link); and that didnt work so i now have tried to connect from wwithin the class as follows...

<?


class headlineClass{
### headlines.php - added [v1.12]
function listHeadlines($tpath, $c, $lim){
// Automatically get $tpath to avoid possible security holes
$tpath = realpath(__FILE__);
$tpath = substr($tpath,0,strrpos($tpath,DIRECTORY_SEPARATOR)+1);
if(file_exists($tpath . "cn_dbdefs.php")) {
// Include MySQL Definitions
require_once($tpath . "cn_dbdefs.php");
} else {
if(file_exists("" . $tpath . "install.php")) {
include_once($tpath . "install.php");
exit;
} else {
print E("Installation file not found. Please upload 'install.php' to run the necessary upgrades for your new version.");
}
}

// Directory the images are in
$imgdir = "images/";

// Make the default database connection
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Unable to connect to MySQL server.<br>" . mysql_error());
mysql_select_db($dbname, $link) or die ("Could Not Connect to Selected Database.<br>" . mysql_error());

// Table prefix name
$t_prefix = "cn";
// Table names
$t_news = $t_prefix . "_news";
$t_user = $t_prefix . "_users";
$t_cats = $t_prefix . "_cats";
$t_conf = $t_prefix . "_config";
$t_coms = $t_prefix . "_comments";
$t_words = $t_prefix . "_words";
$t_img = $t_prefix . "_images";

// LANGEUAGE setting
$langFile = 'en.php';
require_once($tpath . 'lang' . DIRECTORY_SEPARATOR . $langFile);
// Check if a connection to the database was established
if(!isset($link)) {
die("Please make sure the \"\$tpath\" veriable is the root path to where 'headlines.php' is on your server.");
}
// Set limit for number of items displayed
if(!isset($lim)) { $lim = "5"; }

$result = mysql_query("SELECT * FROM $t_news WHERE cat = '$c' ORDER BY date DESC LIMIT 0, $lim", $link);
echo "<ul>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// HTML OUTPUT................
echo "<li>" . $row['subject'] . "</li>";

}
echo "</ul>";
mysql_free_result($result);

}

}
?>

It works the first time but now i get the error...

Warning: mysql_connect() [function.mysql-connect]: Access denied for user '******'@'localhost' (using password: NO) in /home/***/****/LatestNews/allHeadlines.php on line 26
Unable to connect to MySQL server.

link 26 is where $link = mysql_connect($d.... occurs
 
Assuming $dbpass gets set in: require_once($tpath . "cn_dbdefs.php");


It will only do so once as per the require_[red]once[/red] function. So the next time you instantiate your class, the require is no longer executed. Change that to just require() instead.

I would also advise you to post this in forum434, as it is clearly becoming more about PHP than MYSQL.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top