2

这是我的功能

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?需要任何帮助和建议。谢谢 :)

4

5 回答 5

4

对于初学者,不要使用global关键字,而是注入您需要的变量。其次,你为什么不返回一个数组?:

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'];
于 2012-08-26T14:47:39.977 回答
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];
于 2012-08-26T14:59:08.427 回答
1
return array(
    "lname" => $lname,
    "fname" => $fname,
    "mi" => $mi
);
于 2012-08-26T14:48:23.517 回答
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'];
}

未经测试,但我认为它应该工作

于 2012-08-26T14:48:42.830 回答
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'];
}
于 2012-08-26T14:49:25.980 回答