Page 1 of 1

Function with Query

Posted: Wed Dec 28, 2005 10:56 pm
by AliasBDI
I have function that executes a query in order to echo a record. Here is the code:

Code: Select all

function getTopic($topicID){
	mysql_select_db($database_econtrolt411, $econtrolt411);
	$query_getTopic = "SELECT * FROM var_topic WHERE topicID='$topicID'";
	$getTopic = mysql_query($query_getTopic, $econtrolt411) or die(mysql_error());
	$row_getTopic = mysql_fetch_assoc($getTopic);
	$totalRows_getTopic = mysql_num_rows($getTopic);
	echo $row_getTopic['topicNAME'];
}
I have echoed out my query and the variable $topicID is indeed passing successfully. However, I get these two errors:
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in c:\domains\domain.com\....\functionsLIST.php on line 97

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in c:\domains\domain.com\....\functionsLIST.php on line 100
Any ideas as to what is going wrong?

(*paths were covered for security in the error warning, but they are correct*)

Posted: Wed Dec 28, 2005 11:53 pm
by neophyte
You don't have a valid mysql resource. Which means the function doesn't think you are connected to the db. You probably are outside the function. If you have a $foo = mysql_connect(). Put $foo in the function by adding the following line inside the function:

Code: Select all

global $foo;


Hope that helps

Posted: Thu Dec 29, 2005 6:54 am
by Jenk
you can also not specify a link and mysql_query() will find the exiting connection (if there is one).

Posted: Thu Dec 29, 2005 7:00 am
by BDKR
You may want to consider some error logic in there as well. :wink:

Posted: Thu Dec 29, 2005 8:05 am
by AliasBDI
Thanks. I have the connection set higher in the page and assumed that it would work. But I took your advice and placed it inside the function and it works perfectly.