douxi1968 2013-02-24 15:01
浏览 8
已采纳

在PHP类之外的索引中使用var [关闭]

How can i acces the $pos var outside a class? so i can use it in my index like: echo $pos i also icluded this class in my index.

public function CheckProfile(){

    $get = $this->db->query("SELECT positive, posts FROM users WHERE user='". $_SESSION['user'] ."'");

    while($row = $get->fetch(PDO::FETCH_ASSOC))
        {
            return $pos = $row['post'];
        }

    }
  • 写回答

1条回答 默认 最新

  • duandou8120 2013-02-24 17:27
    关注

    As everybody told you, there are many wrong things going on in your code. You should learn how to use Classes and Objects better. Anyways, here is something to start with:

    Assuming you have a Class, let's define a public method:

    public function CheckProfile($user) {
    
        $ret = false;
    
        $stmt = $this->db->prepare("SELECT positive, posts FROM users WHERE user = :user");
        $stmt->bindParam(':user', $user, PDO::PARAM_STR);
    
        if ($stmt->execute())   {
            $ret = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }
    
        return $ret;
    }
    


    Then, from your php file, use it like that:

    $UserData = $YourClassInstance->CheckProfile($_SESSION['user']);
    if ($UserData === false) {
        echo 'Something went wrong...';
    } else {
        print_r($UserData);
    }
    

    Start from this and try to accomplish what you really need.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致