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

发布于 2024-11-19 09:53:54 字数 749 浏览 0 评论 0原文

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

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

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
  } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囚我心虐我身 2024-11-26 09:53:54
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*/
}
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*/
}
滥情空心 2024-11-26 09:53:54

目前您的 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
}

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
}
予囚 2024-11-26 09:53:54

首先,我觉得有必要说您应该使用 PDO,特别是因为 PDOStatement::fetchAll 可以完成您想要的大部分操作。 本教程看起来相对不错。

这里有一些重写,但它会做你想做的一切:

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

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
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文