如何从数据库中选择并使用函数将值存储在数组中?

时间:2011-07-12 13:49:54

标签: php mysql

我想从一个表中选择,获取所有值并将它们放在一个数组中,返回该函数,并在另一个文件中调用它,并在我调用另一个函数时循环遍历该数组,并为其提供数组值。感谢

<?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()返回一个数组,我在另一个文件中调用它

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

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

3 个答案:

答案 0 :(得分: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;
}

在其他文件中:

$final = getusers();
foreach ($final as $user) {
  /*do what you want*/
}

答案 1 :(得分:0)

目前您的getusers()功能不正确

您可以调整getusers()函数以返回用户数组,如下所示:

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;
}

然后在另一个文件中,您可以包含以下内容:

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

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

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

答案 2 :(得分:0)

首先,我觉得有必要说你应该使用PDO,特别是因为PDOStatement::fetchAll完成了你想要的大部分工作。 This tutorial看起来相对不错。

这是一个重写,但它会做你想要的一切:

// 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;
}

在第二个文件中:

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
}