duanji9311 2014-12-02 00:01
浏览 32
已采纳

无法回显主页上的用户数据

This is the code for my function

 public function getuserData() {
  session_start();
  $username = $_SESSION['user']; 
  $getData = $this->db->prepare("SELECT * FROM users WHERE username=?");
  $getData->bindParam(1, $username);
  $getData->execute();
  $row= $getData->fetch(PDO::FETCH_ASSOC);
  $email = $row['email'];
  $user = $row['username'];
  echo $email. '<br />';
  echo $user;
}

<?php

require_once "class/user.php";

echo $user;
echo $email;


$object = new User();
$object->getUserData();

?>

and here is my home.php, the username, and email echo out but only from the function, if i try to do echo $email; on the home.php it doesn't print. I need to print it out on the home page so I can style the output.

Already tried starting session on home.php and it still doesn't work, as I already started it in the getUserData() function.

  • 写回答

1条回答 默认 最新

  • douli4337 2014-12-02 00:12
    关注

    Those variables are only in the scope of the function itself, if you want to access that information, you'd have to return the values from the function.

    user.php

    public function getUserData() {
      session_start();
      $username = $_SESSION['user']; 
      $getData = $this->db->prepare("SELECT * FROM users WHERE username=?");
      $getData->bindParam(1, $username);
      $getData->execute();
      $row= $getData->fetch(PDO::FETCH_ASSOC);
    
      return $row;
    }
    

    home.php

    <?php
    
    require_once "class/user.php";
    
    $object = new User();
    $userInfo = $object->getUserData();
    
    echo $userInfo['username'] . '<br>';
    echo $userInfo['email];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测