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

Sui Go
  • 463
  • 2
  • 12
  • 31

5 Answers5

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'];
Community
  • 1
  • 1
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • 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 – Sui Go Aug 26 '12 at 14:51
  • 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. – PeeHaa Aug 26 '12 at 14:52
  • thanks sir! but how can i retrieve every each of the values like i want to echo the names: echo $lname.', '.$fname.' '.$mi.'.'? – Sui Go Aug 26 '12 at 15:02
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];
1
return array(
    "lname" => $lname,
    "fname" => $fname,
    "mi" => $mi
);
Sepster
  • 4,800
  • 20
  • 38
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

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
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'];
}
Samson
  • 2,801
  • 7
  • 37
  • 55