wwfc_barmy_army Posted November 8, 2009 Share Posted November 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180763-solved-php-function-not-returning-value/ Share on other sites More sharing options...
mrMarcus Posted November 8, 2009 Share Posted November 8, 2009 what is $sect? what does it equal? put single quotes around it in the query: $sql="SELECT sect_name FROM sect WHERE sect_id = '$sect'"; Quote Link to comment https://forums.phpfreaks.com/topic/180763-solved-php-function-not-returning-value/#findComment-953674 Share on other sites More sharing options...
PFMaBiSmAd Posted November 8, 2009 Share Posted November 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180763-solved-php-function-not-returning-value/#findComment-953675 Share on other sites More sharing options...
wwfc_barmy_army Posted November 8, 2009 Author Share Posted November 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180763-solved-php-function-not-returning-value/#findComment-953685 Share on other sites More sharing options...
Andy-H Posted November 8, 2009 Share Posted November 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180763-solved-php-function-not-returning-value/#findComment-953690 Share on other sites More sharing options...
wwfc_barmy_army Posted November 8, 2009 Author Share Posted November 8, 2009 Thanks. I got it now. Quote Link to comment https://forums.phpfreaks.com/topic/180763-solved-php-function-not-returning-value/#findComment-953745 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.