Jump to content

[SOLVED] PHP Function not returning value


wwfc_barmy_army

Recommended Posts

Hello.

 

I have this function:

<?php
function getsectfromid($sect){
$sql="SELECT sect_name FROM sect WHERE sect_id = $sect";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_query($result);

global $sectname;

$sectname = $row['sect_name'];

return $sectname;
}
?>

 

Although I am getting the error:

Unknown column '$sect' in 'where clause'

 

I cant figure out what it's saying $sect is an unknown column as i've specified the column as 'sect' not '$sect'. Any ideas?

 

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/180763-solved-php-function-not-returning-value/
Share on other sites

It is highly likely that your variable $sect contains the word '$sect'. Echo $sql to see exactly what is in it. What is your code that is calling that function?

 

Please DON"T use the globals keyword in your function. For what you are doing, it has no effect and it should not be used in any case.

It is highly likely that your variable $sect contains the word '$sect'. Echo $sql to see exactly what is in it. What is your code that is calling that function?

 

Please DON"T use the globals keyword in your function. For what you are doing, it has no effect and it should not be used in any case.

 

Thats what I thought but if I don't use the global part then when trying to call the returned variable on the page that I called the fuction on i get this error:

 Notice: Undefined variable: sectname in...

 

Any ideas about that?

 

Think I have sorted the SQl problem.

You are globalising the variable before it exists, then also returning it.

 

Jus remove the global line and when you call the function, set it to a variable like so:

$sectname = getsectfromid($id); // obviously change $id to the required variable in your script

 

That way $sectname in the scope where the function is called is set to the return value, global is not necessary.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.