EchoFool Posted January 20, 2009 Share Posted January 20, 2009 I have a function i am trying to call in a script but i get an undefined variable every time and I don't know why... this is what i have..: <?php function collect(){ $Get = mysql_query("SELECT Username from users WHERE UserID='{$_SESSION['UserID']}'") Or die(mysql_error()); $row = mysql_fetch_assoc($Get); $Username = $row['Username']; } collect(); Echo $Username ?> My error: Notice: Use of undefined constant collect - assumed 'collect Notice: Undefined variable: Username in test.php on line 15 What did i do wrong? Quote Link to comment https://forums.phpfreaks.com/topic/141668-solved-help-with-a-function-call/ Share on other sites More sharing options...
jjacquay712 Posted January 20, 2009 Share Posted January 20, 2009 Try this: <?php function collect(){ $Get = mysql_query("SELECT Username from users WHERE UserID='{$_SESSION['UserID']}'") Or die(mysql_error()); $row = mysql_fetch_assoc($Get); return $row['Username']; } echo collect(); ?> Variables inside functions are destroyed after the function is run, but you can return the user name variable... Quote Link to comment https://forums.phpfreaks.com/topic/141668-solved-help-with-a-function-call/#findComment-741570 Share on other sites More sharing options...
EchoFool Posted January 20, 2009 Author Share Posted January 20, 2009 Variables inside functions are destroyed after the function is run, but you can return the user name variable... Hmm is there no way to load variables in a function to pass them back to use at my own disposal when needs be? Example is shown below: <?php function collect(){ $Get = mysql_query("SELECT Username,Level from users WHERE UserID='{$_SESSION['UserID']}'") Or die(mysql_error()); $row = mysql_fetch_assoc($Get); $Username = $row['Username']; $Level = $row['Level']; } collect(); Echo $Username; ?> <br><br> <p align=right> <?php Echo 'Level '.$Level; ?> </p> ?> Obviously from what you said variables are destroyed so is there any work arounds ? Quote Link to comment https://forums.phpfreaks.com/topic/141668-solved-help-with-a-function-call/#findComment-741585 Share on other sites More sharing options...
Zane Posted January 20, 2009 Share Posted January 20, 2009 you can make them global...not the most secure of programming strategies, but it will work function collect(){ $Get = mysql_query("SELECT Username,Level from users WHERE UserID='{$_SESSION['UserID']}'") Or die(mysql_error()); $row = mysql_fetch_assoc($Get); global $Username = $row['Username']; global $Level = $row['Level']; } collect(); Echo $Username; Quote Link to comment https://forums.phpfreaks.com/topic/141668-solved-help-with-a-function-call/#findComment-741595 Share on other sites More sharing options...
trq Posted January 20, 2009 Share Posted January 20, 2009 you can make them global Don't do that. Quote Link to comment https://forums.phpfreaks.com/topic/141668-solved-help-with-a-function-call/#findComment-741596 Share on other sites More sharing options...
EchoFool Posted January 20, 2009 Author Share Posted January 20, 2009 you can make them global Don't do that. Why is that then ? And im assuming if thats the only way and it's not recommended I can't place my query in a function to call variables when needed. Thats a shame, maybe a nice work around will be store php scripts in database and call them with a query. Either that or I'm going too over the top with reducing script sizes hehe Quote Link to comment https://forums.phpfreaks.com/topic/141668-solved-help-with-a-function-call/#findComment-741599 Share on other sites More sharing options...
.josh Posted January 20, 2009 Share Posted January 20, 2009 just return the variable (or variables, as an array) and assign the function call to something. function something () { $array = array('a','b','c'); return $array; } $foobar = something(); echo $foobar[0]; // output: a echo $foobar[1]; // output: b echo $foobar[2]; // output: c Quote Link to comment https://forums.phpfreaks.com/topic/141668-solved-help-with-a-function-call/#findComment-741603 Share on other sites More sharing options...
EchoFool Posted January 20, 2009 Author Share Posted January 20, 2009 Thanks will do that.... by the way if possible why is global variables not a good idea? Quote Link to comment https://forums.phpfreaks.com/topic/141668-solved-help-with-a-function-call/#findComment-741610 Share on other sites More sharing options...
Zane Posted January 20, 2009 Share Posted January 20, 2009 your best bet in this would be to return your $row array ... from collect like so function collect(){ $Get = mysql_query("SELECT Username from users WHERE UserID='{$_SESSION['UserID']}'") Or die(mysql_error()); return $row = mysql_fetch_assoc($Get); } $stuff = collect(); echo $stuff['Username']; Also since you're only searching for one thing in the database, you might as well just use mysql_result so .... function collect(){ $Get = mysql_query("SELECT Username from users WHERE UserID='{$_SESSION['UserID']}'") Or die(mysql_error()); return $theUsername = mysql_result($Get, 0); } $thePersonsUsername = collect(); echo $thePersonsUsername Quote Link to comment https://forums.phpfreaks.com/topic/141668-solved-help-with-a-function-call/#findComment-741613 Share on other sites More sharing options...
Zane Posted January 20, 2009 Share Posted January 20, 2009 Thanks will do that.... by the way if possible why is global variables not a good idea? It's just bad practice. This page describes it pretty well http://en.wikipedia.org/wiki/Global_variable Quote Link to comment https://forums.phpfreaks.com/topic/141668-solved-help-with-a-function-call/#findComment-741619 Share on other sites More sharing options...
EchoFool Posted January 20, 2009 Author Share Posted January 20, 2009 Thanks will do that.... by the way if possible why is global variables not a good idea? It's just bad practice. This page describes it pretty well http://en.wikipedia.org/wiki/Global_variable Thank you Quote Link to comment https://forums.phpfreaks.com/topic/141668-solved-help-with-a-function-call/#findComment-741633 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.