0

I'm attempting to use functions to get certain data from database. For example, I want to get info from an user with ID 1.

try {
    $connection = new PDO("mysql:host=localhost;dbname=database", "root", "password");
}
catch (PDOException $e) {
    die("Error: " . $e->getMessage());
}

$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

function getUser($id) {
    global $connection;
    $query = $connection->prepare("SELECT * FROM accounts WHERE ID = '$id'");
    $query->execute();

    while($row = $query->fetch()) {
        echo $row['playername'];
        $user[] = $row;
    }
}

And then in my index.php.

include 'inc/db.php';

getUser("1");
foreach($user AS $user) {
    echo $user['ID'];
}

The first echo works, I get the username displayed, but the foreach doesn't echo anything. I tried to var_dump($user); but ended up getting NULL.

3
  • 1
    Doesn't make much sense to do foreach($var as $var). That is, to use the same var name. Commented Feb 28, 2014 at 15:46
  • I'm not that good in PHP, still studying. What would you suggest me to use to get data from an user? Commented Feb 28, 2014 at 15:47
  • You need to learn about PHP variable scope. $user inside your getUser method is COMPLETELY different from the (undefined) $user you're trying to use in the foreach loop. Commented Feb 28, 2014 at 15:47

2 Answers 2

2

You need to have:

function getUser(...) {
  ...
  $user = array();
  while(...) {
      $user[] = $row;
  }
  return $user;
}

And then in your main code:

$users = getUser(1);
foreach($users as $user) { .... }

Right now you're defining local variables and then not returning them, so they're lost when the method exits. And then not capturing any possible returned values anyways, making your code basically pointless.

Sign up to request clarification or add additional context in comments.

1 Comment

I attempted to echo value with the code you provided, but I am ending up getting values such as "117744KKhhhhhhhh1111001111112211551122" which are not the values that are in the database.
0

Your problem is that you are writing too much code. PHP can't process so much, chokes and dies.
All you actually need is

$pdo = new PDO("mysql:host=localhost;dbname=database", "root", "password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

function getUser($id)
{
    global $pdo;

    $query = $pdo->prepare("SELECT * FROM accounts WHERE ID = ?");
    $query->execute(array($id));
    return $query->fetch();
}

$user = getUser(1);
echo $user['playername'];

to make it little more serious, you should use prepared statement to pass variable into query and return data from the function.

2 Comments

"php can't process so much and dies"? Huh? HUH!?
This works great for getting a single item. How about if I want to get all users and use foreach method for that?

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.