如何使用PHP从foreach循环返回多个值

时间:2012-08-26 14:43:35

标签: php function

这是我的功能

function GetUser($id)
{
    global $pdo;
    $stmt = $pdo->prepare('SELECT lname,fname,mi FROM user WHERE id = :id LIMIT 1');
    $stmt->execute(array(':id'=>$id));
    foreach($stmt as $name){
        $lname = $name['lname'];
        $lname = $name['fname'];
        $mi = $name['mi'];
    }

    return //what to put here?
}

这是我使用函数

的代码
include 'function.php';
$names = GetUser($_SESSION['id']);
//what's next?

如何从函数中检索$ lname,$ fname和$ mi?需要任何帮助和建议。谢谢:))

5 个答案:

答案 0 :(得分:3)

对于初学者,请不要使用global keyword,而是注入您需要的变量。第二个为什么不返回数组?:

function getUser($pdo, $id)
{
    $stmt = $pdo->prepare('SELECT lname,fname,mi FROM user WHERE id = :id LIMIT 1');
    $stmt->execute(array(':id'=>$id));

    $result = array();
    foreach($stmt as $name){
        $result['lname'] = $name['lname'];
        $result['fname'] = $name['fname'];
        $result['mi']  = $name['mi'];
    }

    return $result;
}

$result = getUser($pdo, 1);
var_dump($result);

请注意,这只会返回最后结果。如果你想要这一切:

function getUser($pdo, $id)
{
    $stmt = $pdo->prepare('SELECT lname,fname,mi FROM user WHERE id = :id LIMIT 1');
    $stmt->execute(array(':id'=>$id));

    return $stmt->fetchAll();
}

$result = getUser($pdo, 1);
var_dump($result);

另请注意,我已将您的功能名称以普通字母而非首字母开头。 “正常”命名约定是类以大写开头,而函数/方法以普通大小开始。

如果您想根据第一个解决方案检索信息:

echo $result['lname'];

如果您想根据第二种解决方案检索信息,请执行以下操作:

echo $result[0]['lname'];

答案 1 :(得分:3)

function GetUser($id)
{
    global $pdo;
    $stmt = $pdo->prepare('SELECT lname,fname,mi FROM user WHERE id = :id LIMIT 1');
    $stmt->execute(array(':id'=>$id));
    foreach($stmt as $name){
        $lname = $name['lname'];
        $lname = $name['fname'];
        $mi = $name['mi'];
    }

    return array(
        "$lname" => $lname,
        "$fname" => $fname,
        "$mi" => $mi
     );
}

这是下一部分:

include 'function.php';
$myArray = GetUser($_SESSION['id']);
$fname = $myArray["$fname"];
$lname = $myArray["$lname"];
$mi = $myArray["$mi"];

或者:

include 'function.php';
$myArray = GetUser($_SESSION['id']);
$fname = $myArray[0];
$lname = $myArray[1];
$mi = $myArray[2];

答案 2 :(得分:1)

return array(
    "lname" => $lname,
    "fname" => $fname,
    "mi" => $mi
);

答案 3 :(得分:1)

function GetUser($id) {
    global $pdo;
    $stmt = $pdo->prepare('SELECT lname,fname,mi FROM user WHERE id = :id LIMIT 1');
    $stmt->execute(array(':id'=>$id));
    return $stmt;
}


include 'function.php';
$names = GetUser($_SESSION['id']);
foreach($names as $name){
    $lname = $name['lname'];
    $lname = $name['fname'];
    $mi = $name['mi'];
}

未经测试,但我认为它应该可行

答案 4 :(得分:1)

function GetUser($id)
{
    global $pdo;
    $stmt = $pdo->prepare('SELECT lname,fname,mi FROM user WHERE id = :id LIMIT 1');
    $stmt->execute(array(':id'=>$id));
   return count($stmt)==1?$stmt[0]:null; //you may have nothing returned from the database so return null
}

然后:

include 'function.php';
$names = GetUser($_SESSION['id']);
if ($names){   //if this is not null then get the properties
echo $names['fname'];
echo $names['lname'];
echo $names['mi'];
}