Using the same connection to database for a few operations

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mic
Forum Newbie
Posts: 3
Joined: Tue Sep 06, 2005 7:17 am

Using the same connection to database for a few operations

Post by mic »

I'm trying to use the same connection for different operations but I keep getting errors
The last error I get is:

Notice: Undefined variable: connection in /mounted-storage/home3/sub007/sc11537-DNSM/michal/forum/delete.php on line 12

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /mounted-storage/home3/sub007/sc11537-DNSM/michal/forum/delete.php on line 12

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /mounted-storage/home3/sub007/sc11537-DNSM/michal/forum/delete.php on line 13


What my code suppose to do:
I created a forum with database. For each topic in my forum there can be a response so when I delete a topic from my forum I check if it has kids and if so I delete all the kids too.
I also check if it has a parent and if so I reduce the number of kids from the "parent topic"

My forum works. Now I need to fix the delete page

If anyone can help
I will appreciate it a lot
Thanks
Michal


the code for the "delete.php":

Code: Select all

<?php 
error_reporting(E_ALL); 


$connection = mysql_connect("mysql2.servage.net", "DBname", "password");
mysql_select_db("DBname", $connection);

//hanle children -delete all children and grandchildren
function children($currentid){
	
	$query="select * from forum1 where parent=" . (int)($currentid) ;
	$result_children= mysql_query($query,$connection);
	$num=mysql_numrows($result_children);
	$i=0;
	while ($i < $num){
		if (mysql_result($result_children,$i,"children")!=0)   children(mysql_result($result_children,$i,"id")); //handle children
		if (mysql_result($result_children,$i,"parent")!="") parent(mysql_result($result_children,$i,"parent")); // hadle parent
		$query = "delete from forum1 where id = " . (int)( mysql_result($result_children,$i,"id") ) ; 
		mysql_query($query)  or die(mysql_error()); 
		$i++;
	}
}
 
//handle parent- decrease number of children
function parent($currentid){ 
  $query="UPDATE forum1 SET children=children-1  WHERE id=" . (int)($currentid);
  mysql_query($query) or die(mysql_error());
}



$query="select * from forum1" ;
$result= mysql_query($query,$connection) or die(mysql_error());
$num=mysql_numrows($result);
$i=0; 
while ($i < $num) {
	$resultid=mysql_result($result,$i,"id");
	if (isset($_REQUEST["checkboxid".$resultid ])) {
		if (mysql_result($result,$i,"children")!=0)  children(mysql_result($result,$i,"id")); // if it has childrens - handle childrens
		if (mysql_result($result,$i,"parent")!="")  parent(mysql_result($result,$i,"parent")); //if it has a parent -handle parent
   		$query = "delete from forum1 where id=" . (int)( mysql_result($result,$i,"id") ); 
		mysql_query($query,$connection) or die(mysql_error()); 
	}
	$i++;
} 

//mysql_close();

//header("Location: cleanforum.php");

//exit;
?>
<html>
<head>
<title>Delete</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
</head>
<body>
</body>
</html>
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

As far as I can see $connection is not a global variable. It is therefore not set within your functions.

You can either pass the connection in as a parameter or use the 'global $connection;' line within your function at the top.
mic
Forum Newbie
Posts: 3
Joined: Tue Sep 06, 2005 7:17 am

Post by mic »

thanks for your help but it still doesn't work even if I add
global $connection;
before creating the connection like this:

global $connection;
$connection = mysql_connect("mysql2.servage.net", "DBname", "password");
mysql_select_db("DBname", $connection);

I still get the same error
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I think you missunderstood.. read this.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

use 'global' not when creating the connection but at the top of your function

Code: Select all

function ... {
  global $connection;
 
  ....

}
mic
Forum Newbie
Posts: 3
Joined: Tue Sep 06, 2005 7:17 am

Post by mic »

I tried it and it is working!!!
Thanks a lot I struggled with it all day.

Do you know why the function "parent" works even without using global variable?

Code: Select all

function parent($currentid){ 
  $query="UPDATE forum1 SET children=children-1  WHERE id=" . (int)($currentid); 
  mysql_query($query) or die(mysql_error()); 
}
doesn't the "update" use the connection too? even if it doesn't directly call the variable "connection"

Michal
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's relying on mysql knowing what connection it used last.
Post Reply