Jump to content

Recommended Posts

Hey guys me again :D 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

<?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.

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.

<?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.

<?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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.