Functions With Queries

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

Functions With Queries

Post by AliasBDI »

I'm having problems with a query placed inside of a function. The query works fine outside, but inside it errors out (mysql_select_db(): supplied argument is not a valid MySQL-Link resource). It occurred to me that maybe queries cannot be placed inside of functions. Is that the case? Here is my code:

Code: Select all

function cBibleBook($val,$sel){
	mysql_select_db($database_econtrolt411, $econtrolt411);
	$query_cbbLIST = "SELECT * FROM var_biblebooks WHERE bookID=$val";
	$cbbLIST = mysql_query($query_cbbLIST, $econtrolt411) or die(mysql_error());
	$row_cbbLIST = mysql_fetch_assoc($cbbLIST);
	if ($sel=1){ 
		$cBibleBook = $row_cbbList['bookTITLE'];
	} elseif ($sel=2){
		$cBibleBook = $row_cbbList['bookSUBJECT'];
	} elseif ($sel=3){
		$cBibleBook = $row_cbbList['bookIMGth'];
	} elseif ($sel=4){
		$cBibleBook = $row_cbbList['bookIMG'];
	}
	mysql_free_result($cbbList);
}
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Yes, queries can be used in function but you will need to pass $database_econtrolt411 and $econtrolt411 to the function for it to work, or make them global
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

I see...

Post by AliasBDI »

I see ... it has to be inside the function as well. How would I make it global? Through global variables? Would that be a security problem?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

If you call:

Code: Select all

mysql_select_db($database_econtrolt411, $econtrolt411);
Outside of the function. Your query will still work fine :)
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Hmmm. Maybe you could help me with this because I am not getting it. Here is my call to the function:

Code: Select all

<?php echo cBibleBook(56,1); echo $cBibleBook; ?>
Here is my function:

Code: Select all

function cBibleBook($val,$sel){
	$hostname_econtrolt411 = "foo";
	$database_econtrolt411 = "foo";
	$username_econtrolt411 = "foo";
	$password_econtrolt411 = "foo"
	$econtrolt411 = mysql_pconnect($hostname_econtrolt411, $username_econtrolt411, $password_econtrolt411) or trigger_error(mysql_error(),E_USER_ERROR);
	mysql_select_db($database_econtrolt411, $econtrolt411);
	$query_cbbLIST = "SELECT * FROM var_biblebooks WHERE bookID=$val";
	$cbbLIST = mysql_query($query_cbbLIST, $econtrolt411) or die(mysql_error());
	$row_cbbLIST = mysql_fetch_assoc($cbbLIST);
	if ($sel=1){ 
		$cBibleBook = $row_cbbList['bookTITLE'];
	} elseif ($sel=2){
		$cBibleBook = $row_cbbList['bookSUBJECT'];
	} elseif ($sel=3){
		$cBibleBook = $row_cbbList['bookIMGth'];
	} elseif ($sel=4){
		$cBibleBook = $row_cbbList['bookIMG'];
	}
	mysql_free_result($cbbLIST);
}
I am wanting to call the function and echo out $cBibleBook. It is not working, however, the query is working fine. What am I missing? I am able to do this in asp but I'm trying to convert it to PHP since the server requires it.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Ahhh. I figured it out. Mispellings in my code. Here is the revised for anyone who cares:

Code: Select all

function cBibleBook($val,$sel){
	$hostname_econtrolt411 = "www.t411.com";
	$database_econtrolt411 = "ect411";
	$username_econtrolt411 = "ect411user";
	$password_econtrolt411 = "ect411pwd";
	$econtrolt411 = mysql_pconnect($hostname_econtrolt411, $username_econtrolt411, $password_econtrolt411) or trigger_error(mysql_error(),E_USER_ERROR);
	mysql_select_db($database_econtrolt411, $econtrolt411);
	$query_cbbLIST = "SELECT * FROM var_biblebooks WHERE bookID=$val";
	$cbbLIST = mysql_query($query_cbbLIST, $econtrolt411) or die(mysql_error());
	$row_cbbLIST = mysql_fetch_assoc($cbbLIST);
	switch ($sel){
	case 1:
		echo $row_cbbLIST['bookTITLE'];
		break;
	case 2:
		echo $row_cbbLIST['bookSUBJECT'];
		break;
	case 3:
		echo $row_cbbLIST['bookIMGth'];
		break;
	case 4:
		echo $row_cbbLIST['bookIMG'];
	}
	mysql_free_result($cbbLIST);
}
Post Reply