Function with Query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Function with Query

Post 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*)
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

you can also not specify a link and mysql_query() will find the exiting connection (if there is one).
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

You may want to consider some error logic in there as well. :wink:
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post 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.
Post Reply