1

I want to select from a table, get all the values and put them in an array, return the function, and call it in another file, and loop through the array whiles i call another function and supply it with the array values. Thanks

<?php
function getusers()
 {
 //db parameters here    
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());  
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());  
$query  = "SELECT user_name FROM userinfo";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{

  return $final[] = $row['user_name'];
}     
    mysql_Close ($conn);
 }
 ?>

getusers() returns an array and I call it in another file

 include('usernames.php');
 getusers();
 while!(end of [$final] array)
 {
    getTax($final[]);
  }

  function getTax($final)
  {
     //statement here
  } 

3 Answers 3

2
function getusers()
{
  //db parameters here    
  mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());  
  mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());  
  $query  = "SELECT user_name FROM userinfo";
  $result = mysql_query($query);
  $return = array();
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))
   {
     $return [] = $row['user_name'];
   }     
 mysql_Close ($conn);
 return $return;
}

And in other file:

$final = getusers();
foreach ($final as $user) {
  /*do what you want*/
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I get this error 'Cannot redeclare getusers()' any help out?
You have function getusers() declared several times. Check your code.
0

Currently your getusers() function is incorrect

You could adjust your getusers() function to return an array of your users like so:

function getusers() {
    //db parameters here    
    mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
    mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
    $query = "SELECT user_name FROM userinfo";
    $result = mysql_query($query);
    $users = array();
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $users[] = $row['user_name'];
    }
    mysql_Close($conn);
    return $users;
}

And then in another file, you can include this:

include('usernames.php');
$users  = getusers();

foreach($users as $user) {
    getTax($user);
}

function getTax($user) {
    // do something with $user here
    echo $user; // prints the username
}

Comments

0

First I feel compelled to say that you should be using PDO, especially since PDOStatement::fetchAll does most of what you want. This tutorial looks relatively decent.

Here's a bit of a re-write, but it will do everything you want it to:

// let yourself cache the connection. That will save you processing time
function getusers( $connection, $dbname = DB_NAME )
{
    if( !$connection ) 
    {
        return FALSE; // we can't do anything here.
    }
    // Selecting a db inside of a function can cause problems for others using
    // your code (if you use $connection as a parameter). Instead explicitly 
    // use the database
    $query  = "SELECT user_name FROM `$dbname`.`userinfo`";
    // you may want to make this cause a return of something other than FALSE, 
    // but you definitely want a way to have some form of early escape here.
    $result = mysql_query($query, $connection) or return FALSE;
    $final = array();
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
       // append the current user_name to the $final array
       $final[] = $row['user_name'];
    } 
    // you'll need to close the connection elsewhere.
    return $final;
}

In the second file:

include('usernames.php');
$conn  = mysql_connect($dbhost, $dbuser, $dbpass);
$users = getusers( $conn, $dbname ) or die( "MySQL Error: " . mysql_error() );
// foreach... as... will loop through your array automatically.
foreach( $users as $user )
{
    // $user refers to each user_name fetched in getusers
    getTax( $user, $connection, $dbname );
}

// and here is where the cost saving of caching your connection is found:
function getTax( $user, $connection, $dbname = DB_NAME )
{
    if( !$connection ) 
    {
        return FALSE; // we can't do anything here.
    }
    $result = mysql_query( "SELECT * FROM TAX_TABLE WHERE USER_NAME = '$user'", 
                           $connection ) or return FALSE;
    // continue as normal
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.