EchoFool Posted March 27, 2010 Share Posted March 27, 2010 Hey, I have a function which returns a query result like this : <?php return(mysql_fetch_assoc($Query); ?> Then my script has: <?php While($row = functioname()) { // do stuff } ?> But for some reason this method does not work... does any one know how i can do the idea im trying to do cos currently it just loops for infinity.. Quote Link to comment https://forums.phpfreaks.com/topic/196718-while-loops-from-function-returns/ Share on other sites More sharing options...
teamatomic Posted March 27, 2010 Share Posted March 27, 2010 try: $result = (mysql_fetch_assoc($Query); return $result; HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/196718-while-loops-from-function-returns/#findComment-1032781 Share on other sites More sharing options...
EchoFool Posted March 27, 2010 Author Share Posted March 27, 2010 Ok tried this: But i get unlimited / endless while looping <?php function listfriends($UserID){ $SELECT = mysql_query("SELECT ContactID FROM social WHERE UserID='$UserID' AND Type='Friend'") Or die(mysql_error()); $row = mysql_fetch_assoc($SELECT); return ($row); } while($row = listfriends($_SESSION['Current_User'])){ getusername($row['ContactID']).'<a href="#">[Mail]</a> <a href="#">[Remove]</a><br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/196718-while-loops-from-function-returns/#findComment-1032789 Share on other sites More sharing options...
trq Posted March 27, 2010 Share Posted March 27, 2010 That code isn't going to work because each time you call it, it executes completely from the beginning again, hence you'll get a never ending loop. You would need to use a static variable, though I'm not going to really recommend this approach either. The code is terrible. If your listfreinds() function was part of a class / object this would be much simpler, as then you can implement the state you need. Quote Link to comment https://forums.phpfreaks.com/topic/196718-while-loops-from-function-returns/#findComment-1032814 Share on other sites More sharing options...
EchoFool Posted March 27, 2010 Author Share Posted March 27, 2010 Well the list friends thing is used on many subdomains so a function was a must. Should i just make it echo from the function instead then =/ ? Quote Link to comment https://forums.phpfreaks.com/topic/196718-while-loops-from-function-returns/#findComment-1032815 Share on other sites More sharing options...
trq Posted March 27, 2010 Share Posted March 27, 2010 Just have it return an array and loop through that. I'm interested in seeing this getusername() function as well as it would appear to me that this is going to make yet more queries to the database. This code is terribly inefficient if that is indeed the case. Quote Link to comment https://forums.phpfreaks.com/topic/196718-while-loops-from-function-returns/#findComment-1032818 Share on other sites More sharing options...
EchoFool Posted March 27, 2010 Author Share Posted March 27, 2010 Im assuming your suggesting here - i do an inner join on contactid on social table and user id in the user table to get username ? Quote Link to comment https://forums.phpfreaks.com/topic/196718-while-loops-from-function-returns/#findComment-1032821 Share on other sites More sharing options...
trq Posted March 27, 2010 Share Posted March 27, 2010 Something like that yeah. Quote Link to comment https://forums.phpfreaks.com/topic/196718-while-loops-from-function-returns/#findComment-1032822 Share on other sites More sharing options...
EchoFool Posted March 27, 2010 Author Share Posted March 27, 2010 Well "getusername" checks if userid exists in usertable and checks if they are staff (as staff are different coloured username) and also gets the username. unless i could some how check across all 3 tables in one query but i always thought calling a function was the efficient method as i can call it all over the site without duplicated lines of code =/ Quote Link to comment https://forums.phpfreaks.com/topic/196718-while-loops-from-function-returns/#findComment-1032823 Share on other sites More sharing options...
Zane Posted March 27, 2010 Share Posted March 27, 2010 You'd probably be better off returning the mysql_query() output. If you return the mysql_fetch_ functions... you'll only get the first row.. a single-dimensional array... i.e: there's no reason to loop through it. function listfriends($UserID){ return mysql_query("SELECT ContactID FROM social WHERE UserID='$UserID' AND Type='Friend'") Or die(mysql_error()); } $row = mysql_fetch_assoc(listfriends($_SESSION['Current_User']))); echo getusername($row['ContactID']).'[Mail] [Remove] '; ?> Quote Link to comment https://forums.phpfreaks.com/topic/196718-while-loops-from-function-returns/#findComment-1032827 Share on other sites More sharing options...
EchoFool Posted March 27, 2010 Author Share Posted March 27, 2010 Thanks Zanus - so would an inner join be more efficient then =/ Because if so i have to re-assess my use of functions Quote Link to comment https://forums.phpfreaks.com/topic/196718-while-loops-from-function-returns/#findComment-1032829 Share on other sites More sharing options...
trq Posted March 27, 2010 Share Posted March 27, 2010 Thanks Zanus - so would an inner join be more efficient then =/ Because if so i have to re-assess my use of functions Of course an inner join is going to be more efficient. Functions simply wrap pieces of code, if those pieces of code are making unnecessary queries they are inefficient. Quote Link to comment https://forums.phpfreaks.com/topic/196718-while-loops-from-function-returns/#findComment-1032831 Share on other sites More sharing options...
Zane Posted March 27, 2010 Share Posted March 27, 2010 You'd probably be better off returning the mysql_query() output. actually I take that back.. if you return the mysql_query function it'll just do the same thing. Since your starting a new resource each time you call the function. I guess you could get away with passing it as a reference or something though.... (anything but global) function listfriends($UserID){ $SELECT = mysql_query("SELECT ContactID FROM social WHERE UserID='$UserID' AND Type='Friend'") Or die(mysql_error()); while($row[] = mysql_fetch_assoc ($_SESSION['Current_User'])); return ($row); } $allFriends = listfriends($_SESSION['Current_User']); foreach($allfriends as $id=>$firend) { getusername($allfriends['ContactID']).'[Mail] [Remove] '; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/196718-while-loops-from-function-returns/#findComment-1032832 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.