2

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 :)

3
  • What's wrong with using a multi-dimensional array? Commented Aug 26, 2012 at 14:45
  • I recommend taking an object-oriented approach and creating a User object which wraps the user values. Alternatively, you can just return the three values in an array. Commented Aug 26, 2012 at 14:45
  • you can use std class as well for this purpose Commented Aug 26, 2012 at 14:50

5 Answers 5

4

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'];
Sign up to request clarification or add additional context in comments.

3 Comments

yes i am a starter. why should I not use global? i have many functions that's why I use global so that i will not use that same line of code over and over again? hehe :) thanks
No worries. Everybody has to start somewhere. :-) See the linked post at the top of my answer and also see the comments/discussion underneath that answer.
thanks sir! but how can i retrieve every each of the values like i want to echo the names: echo $lname.', '.$fname.' '.$mi.'.'?
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
     );
}

This is what's next part:

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

Or:

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

3 Comments

see the revised version above. @SuiGo
choose this one as answer if it helped you @SuiGo :)
@DerikNelson You are running your return array() { } as a function which it isn't
1
return array(
    "lname" => $lname,
    "fname" => $fname,
    "mi" => $mi
);

Comments

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'];
}

Not tested, but I think it should work

Comments

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
}

Then:

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'];
}

1 Comment

thanks, but which is better using this type of approach or the array ? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.