Help with a PDO query

Hello Peeps,

I have a PDO query that I have put into a function. However i’m confused as to how I output the values in the funtion so they can be returned and echoed to my page ie:


function dbQuery ($dbc) {
  $QUERY = $dbc->query("SELECT * from table"); 
  $QUERY->setFetchMode(PDO::FETCH_ASSOC);
  $ROW = $QUERY->fetch(); 
  $output = $ROW['name'];
  $output = $ROW['age'];
  $output = $ROW['weight'];
  $output = $ROW['height'];
  return $output;
}

Then on my page display them


<? // Somehow grab contents of $output from the function and display individually??;?>
Name: <?php echo $ROW['name'];?><br />
Age: <?php echo $ROW['age'];?><br />
Weight: <?php echo $ROW['weight'];?><br />
Height: <?php echo $ROW['height'];?><br />

Thanks

Try something like this…


<?php
error_reporting(-1);
ini_set('display_errors', true);

function get_users(PDO $db){
  $users = array();
  foreach($db->query('SELECT name, age, weight, height FROM users') as $user){
    array_push($users, $user);
  }
  return $users;
}

$db = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'username', 'password');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    <title>demo</title>
  </head>
  <body>
    <?php foreach(get_users($db) as $user): ?>
      <div class="user">
        <ul>
          <li>Name: <?php echo $user['name']; ?></li>
        </ul>
      </div>
    <?php endforeach; ?>
  </body>
</html>

Thanks!