Page 1 of 1

Functions With Queries

Posted: Wed Apr 05, 2006 11:20 am
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);
}

Posted: Wed Apr 05, 2006 11:26 am
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

I see...

Posted: Wed Apr 05, 2006 11:43 am
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?

Posted: Wed Apr 05, 2006 11:46 am
by hawleyjr
If you call:

Code: Select all

mysql_select_db($database_econtrolt411, $econtrolt411);
Outside of the function. Your query will still work fine :)

Posted: Wed Apr 05, 2006 12:00 pm
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.

Posted: Wed Apr 05, 2006 2:58 pm
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);
}