Return data from a function?

Asked

Viewed 41 times

0

How can I return data from a function, in case I want to do a foreach of an sql query that is in a function:

index php.

<?php
    require_once "funcs/functions.php";
    $userdata = get_user_data();
    foreach ($userdata as $d) {
    $name = $d["nome"];
    $id = $d["id"];
    $matricula = $d["matricula"];

    echo $name."<br>".$id."<br>".$matricula;
    }

functions.php

function get_user_data(){
  $user = $_SESSION["Login"];
  $pass = $_SESSION["Password"];
  $pdo = cnx();
  $select = $pdo->query("select * from matricula where username = '$user'");
  $select->execute();
  $rowCount = $select->rowCount();
  if ($rowCount == 0):
      header("Location:index.php?no=1");
  else:
      foreach ($select as $a):
          $password = $a["password"];
      endforeach;
      if($pass == $password){
        return $select;
      } else {
          header("Location:index.php?no=1");
      }
  endif;
}
  • This is not already being done?

  • No, it only worked after I used $userdata->execute()

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.