Jump to content

Iterating MySQL Table


neridaj

Recommended Posts

Hello,

 

I have a function that is supposed to return the columns and rows from a db table and pass it on to another function for output. I know this is a routine procedure, but I'm still green with multidimensional arrays and haven't quite got my head around it. Here is the code I have:

 

function get_users()
{
  //extract from the database all the user data
  $conn = db_connect();
  $result = $conn->query( "select username, firstname, lastname, email
                          from user");
  if (!$result)
    return false; 

  //create an array of the user data
  $user_array = array();
  for ($count = 1; $row = $result->mysql_fetch_row(); ++$count) 
  {
    $user_array[$count] = $row[0];
  }  
  return $user_array;
}


function display_users($user_array)
{
  // set global variable, so we can test later if this is on the page
  global $user_table;
  $user_table = true;
?>
  <br />
  <form name='user_table' action='delete_users.php' method='post'>
  <table width=300 cellpadding=2 cellspacing=0>
  <?php
  $color = "#000000";
  echo "<tr bgcolor='$color'><td><strong>User</strong></td>";
  echo "<td><strong>Delete?</strong></td></tr>";
  if (is_array($user_array) && count($user_array)>0)
  {
    while ($row = mysql_fetch_array($user_array, MYSQL_ASSOC))
    {
      if ($color == "#cccccc")
        $color = "#ffffff";
      else
        $color = "#cccccc";

      printf ("<tr bgcolor='$color'><td>%s</td><td>%s</td><td>%s</td><td>%s</td>", $row["username"], $row["firstname"], $row["lastname"], $row["email"]);
      
      echo "</tr>"; 
    }
mysql_free_result($user_array);
  }

 

Thanks for any help,

 

Jason

Link to comment
Share on other sites

I'm not a wizard on arrays but I have found you have to create the array outside the function then call it in the function with "&$my_array" Then it will return your array.

 

So you would have something like this:

//create an array of the user data
$user_array = array();

function get_users($conn,&$user_array)
{
  //extract from the database all the user data
  $conn = db_connect();
  $result = $conn->query( "select username, firstname, lastname, email
                          from user");
  if (!$result)
    return false; 
  for ($count = 1; $row = $result->mysql_fetch_row(); ++$count) 
  {
    $user_array[$count] = $row[0];
  }  
}

 

Hope this helps point you in the right direction...

 

 

Link to comment
Share on other sites

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.