spfoonnewb Posted March 24, 2007 Share Posted March 24, 2007 I created a function that does a lot, and I need the value of it to be inserted into a mysql database.. If I call the function the normal way.. echo function_name(); It returns the value that I want. It wont let me save that value as a variable such as.. $var = function_name(); If I put the function in the mysql insert statement the output is blank. VALUE('".function_name()."') What do I do!? The end result of a function has to run a for each that looks like: foreach ($str as $strval) { echo $strval; } So I cant save the stuff the function does as variable either. Link to comment https://forums.phpfreaks.com/topic/44087-solved-functions-frustrating/ Share on other sites More sharing options...
MadTechie Posted March 24, 2007 Share Posted March 24, 2007 what does <?php var_dump(function_name()); ?> return ? Link to comment https://forums.phpfreaks.com/topic/44087-solved-functions-frustrating/#findComment-214070 Share on other sites More sharing options...
spfoonnewb Posted March 24, 2007 Author Share Posted March 24, 2007 That returns NULL for some reason.. I don't understand .. like I said if I call the function by its name it returns a value.. Link to comment https://forums.phpfreaks.com/topic/44087-solved-functions-frustrating/#findComment-214075 Share on other sites More sharing options...
spfoonnewb Posted March 24, 2007 Author Share Posted March 24, 2007 I changed this from echo to return.. and I get one of the values.. but not all of them from the array. foreach ($str as $strval) { echo $strval; } foreach ($str as $strval) { return $strval; } Link to comment https://forums.phpfreaks.com/topic/44087-solved-functions-frustrating/#findComment-214076 Share on other sites More sharing options...
cmgmyr Posted March 24, 2007 Share Posted March 24, 2007 try something like this: <?php $variable = myfunction(); echo $variable; function myfunction(){ $sum = 2 + 2; return $sum; } ?> Link to comment https://forums.phpfreaks.com/topic/44087-solved-functions-frustrating/#findComment-214077 Share on other sites More sharing options...
cmgmyr Posted March 24, 2007 Share Posted March 24, 2007 sorry didn't see your last post... try something like this: <?php $variable = myfunction(); for($i=0, $i<=count($variable), $i++){ echo $variable[$i]."<br />"; } function myfunction(){ foreach ($str as $strval) { $something[] = $strval; } return $something; } ?> *untested Link to comment https://forums.phpfreaks.com/topic/44087-solved-functions-frustrating/#findComment-214078 Share on other sites More sharing options...
spfoonnewb Posted March 24, 2007 Author Share Posted March 24, 2007 Ok, now that works.. but I need a way to get it to save into the database The following returns the value I need.. but I can't get it in mysql $variable = myfunction(); for ($i=0; $i<=count($variable); $i++) { echo $variable[$i]; } Heres a non-working way of what I mean.. $variable = myfunction(); $test = for ($i=0; $i<=count($variable); $i++) { echo $variable[$i]; } Link to comment https://forums.phpfreaks.com/topic/44087-solved-functions-frustrating/#findComment-214081 Share on other sites More sharing options...
spfoonnewb Posted March 24, 2007 Author Share Posted March 24, 2007 Any way to do that? Link to comment https://forums.phpfreaks.com/topic/44087-solved-functions-frustrating/#findComment-214223 Share on other sites More sharing options...
cmgmyr Posted March 24, 2007 Share Posted March 24, 2007 All you need to do is put a query in where it is echoing it now: mysql_query("UPDATE your_table SET variable = ".$variable[$i]." WHERE something = 'a'"); Link to comment https://forums.phpfreaks.com/topic/44087-solved-functions-frustrating/#findComment-214287 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.