Skip to main content
edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Better way for Displaying a greeting to output an array of size 1one user selected from a database

Source Link

Better way for to output an array of size 1

I execute the following SQL statement to get back an data object into a variable.

  try {
    $getUser = $db->query('select * from user where user.user_id = '.$user_id);
  } catch (Exception $e) {
    echo $e->getMessage();
    die();
  }

from this I print out the information on the page where necessary e.g. the following:

  <?php
  foreach ($getUser as $user) {
    echo '
    <div class="col-wd-12">
      <div class="col bodyHead">Welcome, '.$user['email'].'</div>
    </div>
    ';
  }
  ?>

The question is I know the size of the $getUser array to always be one. Am I doing this the correct way when returning the information? Is there a better way where I do not need to use a foreach loop everytime.