Page 1 of 1

Using the same connection to database for a few operations

Posted: Tue Sep 06, 2005 7:34 am
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>

Posted: Tue Sep 06, 2005 8:06 am
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.

Posted: Tue Sep 06, 2005 11:04 am
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

Posted: Tue Sep 06, 2005 11:08 am
by feyd
I think you missunderstood.. read this.

Posted: Tue Sep 06, 2005 11:08 am
by andre_c
use 'global' not when creating the connection but at the top of your function

Code: Select all

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

}

Posted: Tue Sep 06, 2005 3:53 pm
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

Posted: Tue Sep 06, 2005 8:45 pm
by feyd
it's relying on mysql knowing what connection it used last.