For starters don't use the global
keyword, but inject the variable you need. Second why don't you return an array?:
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);
Note that this will only return the last result. If you want it all:
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);
Also note that I have made your function name starting with a normal letter instead of a capital. The "normal" naming convention is that classes start with a capital, but function / methods with a normal one.
If you want to retrieve the information based on the first solution you would do:
echo $result['lname'];
If you want to retrieve the information based on the second solution you would do:
echo $result[0]['lname'];