EchoFool Posted December 20, 2009 Share Posted December 20, 2009 Is there a quick method to assigning fields to an Array ? Say i have SELECT * FROM table Now say theres 30 field, i don't want to be doing: $UserID = $row['UserID'];$Username = $row['Username']; for all 30 fields. Is there a way to loop them into an array? Would save so much time. Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 20, 2009 Share Posted December 20, 2009 What's wrong with $row['UserID']? It is a perfectly valid variable. By assigning it to another variable $UserID = $row['UserID'], you are just wasting processing time and memory. Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/#findComment-980751 Share on other sites More sharing options...
EchoFool Posted December 20, 2009 Author Share Posted December 20, 2009 What's wrong with $row['UserID']? It is a perfectly valid variable. By assigning it to another variable $UserID = $row['UserID'], you are just wasting processing time and memory. Okay good point on that front - Judging from your answer that means the loop option is not possible ? Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/#findComment-980753 Share on other sites More sharing options...
PFMaBiSmAd Posted December 20, 2009 Share Posted December 20, 2009 Anything involving the creation, manipulation, or outputting of data is possible. That's what computers were created for. What exactly do you want to put into an array and in what form or order? Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/#findComment-980755 Share on other sites More sharing options...
EchoFool Posted December 20, 2009 Author Share Posted December 20, 2009 Okay ill do a small example Table: UserID | Username | Password | Email --------------------------- Query: SELECT * FROM table Now in php currently i do: $array = array($row['UserID'],$row['Username'] etc etc But if theres some 30 + fields in the table, it gets a bit annoying having to write it all out, so i wanted to see if i can create the array from while looping some how - so that i don't have to type out giant arrays all the time. The reason i put them in an array is because this occurs in a function. Which i then place the array in return(). Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/#findComment-980757 Share on other sites More sharing options...
teynon Posted December 20, 2009 Share Posted December 20, 2009 I'm not sure I agree with what you're trying to do, but this will do it: $array=array(); foreach ($row as $key=>$value) { $array[$key]=$value; } Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/#findComment-980762 Share on other sites More sharing options...
EchoFool Posted December 20, 2009 Author Share Posted December 20, 2009 I'm not sure I agree with what you're trying to do, but this will do it: $array=array(); foreach ($row as $key=>$value) { $array[$key]=$value; } I will give it a try - may i ask why you don't agree? Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/#findComment-980766 Share on other sites More sharing options...
trq Posted December 20, 2009 Share Posted December 20, 2009 I'm not sure I agree with what you're trying to do, but this will do it: $array=array(); foreach ($row as $key=>$value) { $array[$key]=$value; } I will give it a try - may i ask why you don't agree? Because you likely don't need it. Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/#findComment-980768 Share on other sites More sharing options...
PFMaBiSmAd Posted December 20, 2009 Share Posted December 20, 2009 Umm. $row, that you fetched from the result set (using mysql_fetch_assoc or mysql_fetch_array) is already an array. Iterating over it to put each piece of it into another array is extremely pointless. Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/#findComment-980770 Share on other sites More sharing options...
EchoFool Posted December 20, 2009 Author Share Posted December 20, 2009 Oh - did not know that, so then i just need to do return($row) in the function ? Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/#findComment-981001 Share on other sites More sharing options...
Buddski Posted December 20, 2009 Share Posted December 20, 2009 Presuming I understand what you are trying to accomplish, then yes.. You want to return the $row.. Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/#findComment-981003 Share on other sites More sharing options...
EchoFool Posted December 20, 2009 Author Share Posted December 20, 2009 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/#findComment-981004 Share on other sites More sharing options...
PFMaBiSmAd Posted December 20, 2009 Share Posted December 20, 2009 What function? You haven't shown enough information about what you are doing or how you are using the result of the query. Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/#findComment-981005 Share on other sites More sharing options...
EchoFool Posted December 20, 2009 Author Share Posted December 20, 2009 This would be my function: <?php function getall ($UserID){ $SELECT = mysql_query("SELECT * FROM users where UserID='$UserID'") Or die(mysql_error()); $row = mysql_Fetch_array($SELECT); return ($row) } ?> Quote Link to comment https://forums.phpfreaks.com/topic/185740-assign-fields/#findComment-981007 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.