Cenron Posted June 7, 2007 Share Posted June 7, 2007 Hey guys me again this time around I am trying to dump my entire user table into a variable so I can go through them with foreach so for example I want do something like this. $result = mysql(query for user data); foreach($result as $users) { echo $users['user']; } and so forth any idea guys? Link to comment https://forums.phpfreaks.com/topic/54539-solved-dumping-a-table-into-a-variable/ Share on other sites More sharing options...
trq Posted June 7, 2007 Share Posted June 7, 2007 <?php if ($result = mysql_query("SELECT uname FROM users") && mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo $row['uname']; } } ?> If this is not what you want to do you'll need to explain in English. Your pseudo code does nothing to help. Link to comment https://forums.phpfreaks.com/topic/54539-solved-dumping-a-table-into-a-variable/#findComment-269783 Share on other sites More sharing options...
Cenron Posted June 7, 2007 Author Share Posted June 7, 2007 Well see what I want to do is dump all the info in the user table into one big array instead of pulling one row at a time like your doing it. The reason I want to do that is because I am using this for a template engine I am writing...so I need to use foreach(); instead of any other loop. Link to comment https://forums.phpfreaks.com/topic/54539-solved-dumping-a-table-into-a-variable/#findComment-269858 Share on other sites More sharing options...
dymon Posted June 7, 2007 Share Posted June 7, 2007 <?php if ($result = mysql_query("SELECT uname FROM users") && mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { $results[] = $row['uname']; } } ?> And now you can extract from the results array using a foreach loop. Link to comment https://forums.phpfreaks.com/topic/54539-solved-dumping-a-table-into-a-variable/#findComment-269878 Share on other sites More sharing options...
Cenron Posted June 7, 2007 Author Share Posted June 7, 2007 You guys are awesome! I have never been part of a forum that actually helps the beginners instead of flaming them, right on guys. Link to comment https://forums.phpfreaks.com/topic/54539-solved-dumping-a-table-into-a-variable/#findComment-270191 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 <?php if ($result = mysql_query("SELECT uname FROM users") && mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { $results[] = $row['uname']; } } ?> And now you can extract from the results array using a foreach loop. On that note if you use that alot it might be wise to create a function. <?php function fetch_multi_array($sql) { // see if a valid resource was passed in $result = (strstr($sql, "Resource"))?$sql:mysql_query($sql); $i=0; // use $i to have some type of an index. while ($row = mysql_fetch_assoc($result)) { $return[$i] = $row; $i++; } mysql_free_result($result); // free the result return $return; } ?> It still has yet to fail me and makes it very easy to not have to duplicate code. Link to comment https://forums.phpfreaks.com/topic/54539-solved-dumping-a-table-into-a-variable/#findComment-270195 Share on other sites More sharing options...
dymon Posted June 7, 2007 Share Posted June 7, 2007 You don't have to use $return[$i], where $i is a counter, as far as $return[] does the same, and if it will be used many times it will save some executing script time. Link to comment https://forums.phpfreaks.com/topic/54539-solved-dumping-a-table-into-a-variable/#findComment-270198 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 You don't have to use $return[$i], where $i is a counter, as far as $return[] does the same, and if it will be used many times it will save some executing script time. It seems I stand corrected, for some reason in the past (maybe with an older php version) using the [] often times did not produce "correct results" at any rate here is a revised function. <?php function fetch_multi_array($sql) { // see if a valid resource was passed in $result = (strstr($sql, "Resource"))?$sql:mysql_query($sql); while ($row = mysql_fetch_assoc($result)) { $return[] = $row; } mysql_free_result($result); // free the result return $return; } ?> But as for the execution time, I doubt it would make a difference unless you are returning like 5,000+ rows each run. Even then I do not think it would slow it down and make the script less efficient. Link to comment https://forums.phpfreaks.com/topic/54539-solved-dumping-a-table-into-a-variable/#findComment-270221 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.