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

Link to comment
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.

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.