Here’s my 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?
}
Here’s my code to use the function
include 'function.php';
$names = GetUser($_SESSION['id']);
//what's next?
How can i retrieve the $lname,$fname and $mi from the function? Need any help and suggestions. Thank you 🙂
For starters don’t use the
globalkeyword, but inject the variable you need. Second why don’t you return an array?:Note that this will only return the last result. If you want it all:
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:
If you want to retrieve the information based on the second solution you would do: