dk4210 Posted March 22, 2011 Share Posted March 22, 2011 Hello Guys, I am trying to understand how to return a value from a function. I just can't get it to work. For example I have a page lets call it index.php and on this page I have a call to a function like this $scat = 2; // Function to choose which query to use what_Table($scat,$w_query); echo "This is the w query" $w_query; I have an included file called general_functions.php and on that page I have this code function what_Table($scat,$w_query){ $ctquery = mysql_query("SELECT w_query FROM ad_category WHERE cat_status='1' AND cat_id='$scat'") or die(mysql_error()); $row = mysql_fetch_array($ctquery); // Stands for which query $w_query = $row['w_query']; return $w_query; The echo $w_query does not return a value. Any help would be appreciated! Thanks, Dan Link to comment https://forums.phpfreaks.com/topic/231381-function-help/ Share on other sites More sharing options...
BluB Posted March 22, 2011 Share Posted March 22, 2011 From what I can see, your code only has a few minor errors. echo "This is the w query" $w_query; Wont work, you'll need to add the two strings together with a . Like this: echo "This is the w query" . $w_query; Also, to get a return value, you just do: function example($i) { $value = $i + 1; return $value; } You do not need to add the return variable to the parameter list. In your example you call "what_Table($scat,$w_query);" without having anything in the $w_query variable, this will give a Notice: Undefined variable: error. So when you call the function, do it like this: $w_query = what_Table($scat); and remove the $w_query from the parameter list. Then it should work just fine and also the echo should work. I hope this helps. Link to comment https://forums.phpfreaks.com/topic/231381-function-help/#findComment-1190793 Share on other sites More sharing options...
AbraCadaver Posted March 22, 2011 Share Posted March 22, 2011 Also, you're not assigning the return of the function to anything. One these will work: $w_query = what_Table($scat); echo "This is the w query" . $w_query; //or echo "This is the w query" . what_Table($scat); Link to comment https://forums.phpfreaks.com/topic/231381-function-help/#findComment-1190881 Share on other sites More sharing options...
dk4210 Posted March 22, 2011 Author Share Posted March 22, 2011 Echo does work, but I don't want it to echo out the value to the screen. I would like it to return the value instead. Link to comment https://forums.phpfreaks.com/topic/231381-function-help/#findComment-1190907 Share on other sites More sharing options...
AbraCadaver Posted March 22, 2011 Share Posted March 22, 2011 $w_query = what_Table($scat); Link to comment https://forums.phpfreaks.com/topic/231381-function-help/#findComment-1190915 Share on other sites More sharing options...
dk4210 Posted March 22, 2011 Author Share Posted March 22, 2011 Hmm Still not working for me. Within the function I get a 1(which is correct) out side the function I get nothing. index.php page (Echo just to test it) // Function to choose which query to use what_Table($scat); $w_query = what_Table($scat); echo" This is outside the function" . $w_query; exit; The function [echo just to test it out] / Choosing cat type function. This is for differnt queries. function what_Table($scat){ $ctquery = mysql_query("SELECT w_query FROM ad_category WHERE cat_status='1' AND cat_id='$scat'") or die(mysql_error()); $row = mysql_fetch_array($ctquery); // Stands for which query $w_query = $row['w_query']; Echo "<br>This is inside the function" . $w_query; Here is the result.. http://screencast.com/t/T2voSWe0zDja Thanks for all your help thus far..I just can't seem to get it working correctly.. Link to comment https://forums.phpfreaks.com/topic/231381-function-help/#findComment-1190939 Share on other sites More sharing options...
AbraCadaver Posted March 22, 2011 Share Posted March 22, 2011 In your original code you had: return $w_query; Put it back! Link to comment https://forums.phpfreaks.com/topic/231381-function-help/#findComment-1190948 Share on other sites More sharing options...
dk4210 Posted March 22, 2011 Author Share Posted March 22, 2011 Perfect!!! Thanks Man.. I like the - hahaah Link to comment https://forums.phpfreaks.com/topic/231381-function-help/#findComment-1190967 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.