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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with function 2

Status
Not open for further replies.

coderwasp

Programmer
Jan 10, 2007
24
US
When I load this page I get the error:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in report.php on line 130 (the line that reads $result= mysql_query($sql,$o_conn);)

I know the query is valid and my datbase connection is correct, so there must be something wrong with the function. What could be wrong?


Code:
function display_assigners($parent, $level) 
{


	$sql="SELECT FirstName, LastName, ID FROM teacher WHERE ParentAdminID=".$adderid;
	$result= mysql_query($sql,$o_conn);
	if(mysql_num_rows($result)==0)
	{
		print  str_repeat('&nbsp;&nbsp;&nbsp;',$level)." -- none.<BR>";
	}
	else
	while ($row = mysql_fetch_array($result)) 
	{
		print str_repeat('&nbsp;&nbsp;&nbsp;',$level).$row['FirstName']." ".$row["LastName"]."<BR>";
		display_assigners($row['ID'], $level+1);
   	}
}
 
where does $adderid get passed to the function?
where does $o_conn get passed to the function?

you should check out the manual about variable scope. for the time being add the following as the first line to "fix" this issue (assuming these two variables are not created in another private scope)

Code:
global $adderid, $o_conn;
 
I tried it but it didn't work.

I'm sorry I forgot to paste this line of code which was right above the function declaration:

Code:
display_assigners($thisoneid,0);

$adderid is a value that is passed from a get variable, it is set earlier in teh page

$o_conn is in an include file. It is a correct connection varaible to the mysql db
 
Ok, I got what you mean about declaring variables, so I moved the connection info inside the function:

Code:
	$o_conn=mysql_pconnect("localhost","testuser","testpassword");
	mysql_select_db('testdb',$o_conn) or die("Couldn't open database");

now when the function is invoked I get the error message Couldn't open database. why is this happening?
 
change
Code:
$o_conn=mysql_pconnect("localhost","testuser","testpassword");
    mysql_select_db('testdb',$o_conn) or die("Couldn't open database");

to this:

$o_conn=mysql_pconnect("localhost","testuser","testpassword");
    mysql_select_db('testdb',$o_conn) or die("Couldn't open database:"[red].mysql_error()[/red]);

to get a more explicit error message. That way you can see why the connection failed.

----------------------------------
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top