johnslater Posted February 3, 2008 Share Posted February 3, 2008 I have a function which is out-putting information from a MySQL my MySQL query has a while loop which is where my problem seems to be. I want to pass the value of a variable out of the while loop and also out of the function to the main script. How is this done? Link to comment https://forums.phpfreaks.com/topic/89238-returning-variables/ Share on other sites More sharing options...
pocobueno1388 Posted February 3, 2008 Share Posted February 3, 2008 Can you post your code? Sounds like you need to use the return keyword. Link to comment https://forums.phpfreaks.com/topic/89238-returning-variables/#findComment-456942 Share on other sites More sharing options...
johnslater Posted February 3, 2008 Author Share Posted February 3, 2008 // Is there a user cookie set? function cookie() { if (isset($_COOKIE["userlogin"])) { $cookie_acti = $_COOKIE["userlogin"]; $result = mysql_query("SELECT * FROM users WHERE activationid='$cookie_acti'"); while($row = mysql_fetch_array($result)) { $cookie_username = $row['username']; $cookie_fname = $row['fname']; $cookie_lname = $row['lname']; return $cookie_username; return $cookie_fname; return $cookie_lname; } } else { $cookie_username = "Guest"; $cookie_fname = "Guest"; $cookie_lname = ""; } } // End cookie function cookie(); I have once used a different method to get 1 item from a database but i dont seem to be able to find it, if you could post another emthod that you think may fix the while loop then that would be great Link to comment https://forums.phpfreaks.com/topic/89238-returning-variables/#findComment-456946 Share on other sites More sharing options...
Stooney Posted February 3, 2008 Share Posted February 3, 2008 in your script: $variable=cookie(); $variable will get the value of the variable you're returning in the function. at the end of your function return $variable; //change $variable to what you want to return to the main script. Link to comment https://forums.phpfreaks.com/topic/89238-returning-variables/#findComment-456952 Share on other sites More sharing options...
johnslater Posted February 3, 2008 Author Share Posted February 3, 2008 It looks like that has fixed my problem. thanks for your help :-) Link to comment https://forums.phpfreaks.com/topic/89238-returning-variables/#findComment-456960 Share on other sites More sharing options...
johnslater Posted February 3, 2008 Author Share Posted February 3, 2008 Is there anyway i can set up all of these varibles then just call them as normal like $cookie_username I currently have this... $cookie_fname = $cookie_fname=cookie(); $cookie_lname = $cookie_lname=cookie(); $cookie_username = $cookie_username=cookie(); $cookie_set = $cookie_set=cookie(); But for some reason the top one cancels out all of the others Link to comment https://forums.phpfreaks.com/topic/89238-returning-variables/#findComment-456976 Share on other sites More sharing options...
johnslater Posted February 3, 2008 Author Share Posted February 3, 2008 After slightly more testing and after putting this at the end of my function like you suggested it appear it only works with one value however i need to call several values from the same function. return $cookie_username; return $cookie_fname; return $cookie_lname; return $cookie_set; Only the top one (username) is being used and no matter which value i try to call it always calls username Link to comment https://forums.phpfreaks.com/topic/89238-returning-variables/#findComment-456980 Share on other sites More sharing options...
Stooney Posted February 3, 2008 Share Posted February 3, 2008 You can only return one variable with return. Your best bet would be to return an array containing all of the data you need. Link to comment https://forums.phpfreaks.com/topic/89238-returning-variables/#findComment-457026 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.