dongyi9783 2012-08-03 13:17
浏览 56
已采纳

PHP OOP返回并调用单个数组值

I'm in the process of learning to use OOP in php, I'm having a few issues with what is probably a really simple solution that I'm just not using the right search terminology to find.

I have my class

class user {

    function getUser() {

        if ($_SESSION['status'] == "authorized") {

            $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );

            $sql = "SELECT * FROM users WHERE username = :username";

            $st = $conn->prepare( $sql );

            $st->bindValue( ":username", $_SESSION['usernames'], PDO::PARAM_STR );

            $st->execute();

            $row = $st->fetch();

            $conn = null;

            return $row;

        }
    }
}

and then in my template file i'm calling the following

$user = new user();
echo $user->getUser->icon;

hopefully the bottom line shows what i'm trying to call, basically in the sql results I'm after just calling $row['icon']; directly from the return array.

is this possible? if so could someone point out the terminology i'm missing

Thanks in advance

  • 写回答

3条回答 默认 最新

  • dongxie3352 2012-08-03 13:20
    关注

    If you are going to keep using that object I would do the following:

    class user {
        public $icon;
    
        function getUser() {
    
        if ($_SESSION['status'] == "authorized") {
    
            $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    
            $sql = "SELECT * FROM users WHERE username = :username";
    
            $st = $conn->prepare( $sql );
    
            $st->bindValue( ":username", $_SESSION['usernames'], PDO::PARAM_STR );
    
            $st->execute();
    
            $row = $st->fetch();
    
            $conn = null;
    
            $this->icon=$row;
    
        }
        }
    }
    

    Then you can use:

    echo $user->icon;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?