drpkcwwav20524605 2015-07-22 16:08
浏览 57

根据函数逐行获取数据库查询结果

This will be long.
I'm making class which will get data about teams - 5 Steam users basing on 32bit SteamIDs stored in database - 1 row = 1 team. I want to get one result, when I specify teamid and all rows, when it's not defined(or when equals 'all'). And here starts the problem, 'cause specified team gives return, but I can't get all rows, it just gives null when var_dumped. I have already created this method:

public static function baseData($teamid = null){
        if(!empty($teamid)){
            if(is_numeric($teamid)){
                DB::getInstance()->get('', 'o90eprrzc3v8', ['53qwi8md3rm7', '=', $teamid]);
            }
            elseif($teamid == 'all'){
                DB::getInstance()->getOrdered('', 'o90eprrzc3v8', '53qwi8md3rm7', 'ASC');
            }
            return DB::getInstance()->results();
        }
        return false;
    }

where DB class' methods I use in Team class looks like this:

public function bquery($sql, $params = array()){
        $this->_error = false;

        if($this->_query = $this->_pdo->prepare($sql)){
            $x = 1;
            if(count($params)) {
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }

            if($this->_query->execute()){
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
            } 
            else{
                $this->_error = true;
            }
        }

        return $this;
    }

    public function action($action, $table, $where = null){
        if(!empty($where)){
            (array)$where;
            if(count($where) === 3){
                $operators = ['=', '>', '<', '>=', '<='];

                $field = $where[0];
                $operator = $where[1];
                $value = $where[2];

                if(in_array($operator, $operators)){
                    $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";

                    if(!$this->bquery($sql, array($value))->error()){
                        return $this;
                    }
                }
            }
            return false;
        }
        elseif(empty($where)){
            $sql = "{$action} FROM {$table}";

            if(!$this->bquery($sql, null)->error()){
                        return $this;
            }
        }
    }
public function get($selector, $table, $where = null){
        if(empty($selector)){
            if(!empty($where)){
                return $this->action('SELECT *', $table, $where);
            }
            else{
                return $this->action('SELECT *', $table);
            }
        }
        else{
            if(!empty($where)){
                return $this->action('SELECT '.$selector.'', $table, $where);
            }
            else{
                return $this->action('SELECT '.$selector.'', $table);
            }
        }
    }

    public function getOrdered($selector, $table, $order, $orderType, $where = null){
        $orderType = strtoupper($orderType);
        if(($selector = '') or (empty($selector))){
            return $this->action('SELECT *', $table, 'ORDER BY '.$order.' '.$orderType.'');
        }
        else{
            return $this->action('SELECT '.$selector.'', $table, 'ORDER BY '.$order.' '.$orderType.'');
        }
    }

    public function results(){
        return $this->_results;
    }
public function count(){
        return $this->_count;
    }

And from this I want to
- loop through results
- return all variables, so I can echo them on index page I've tried to create method for this, but until baseData() doesn't work, I can't check if it's working (it probably won't, as most of apps I write :D):

public static function getSteamData($teamid = 0){
        for($teamid = 0;$teamid<(DB::getInstance()->count());$teamid++){
            $result = self::baseData($teamid);

            $stmid_capt = "STEAM_0:$result->stmidcapt_1:$result->stmidcapt_2";
            $stmid_p2 = "STEAM_0:$result->stmidp2_1:$result->stmidp2_2";
            $stmid_p3 = "STEAM_0:$result->stmidp3_1:$result->stmidp3_2";
            $stmid_p4 = "STEAM_0:$result->stmidp4_1:$result->stmidp4_2";
            $stmid_p5 = "STEAM_0:$result->stmidp5_1:$result->stmidp5_2";

            $stmid64 = [
                'capt' => convertId($result->stmidcapt_2),
                'p2' => convertId($result->stmidp2_2),
                'p3' => convertId($result->stmidp3_2),
                'p4' => convertId($result->stmidp4_2),
                'p5' => convertId($result->stmidp5_2),
            ];

            $stmid3 = [
                'capt' => convertId3($result->stmidcapt_1, $result->stmidcapt_2),
                'p2' => convertId3($result->stmidp2_1, $result->stmidp2_2),
                'p3' => convertId3($result->stmidp3_1, $result->stmidp3_2),
                'p4' => convertId3($result->stmidp4_1, $result->stmidp4_2),
                'p5' => convertId3($result->stmidp5_1, $result->stmidp5_2),
            ];

            $profile_get[0] = Arrays::get('response→players', json_decode(file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.Arrays::get("steamapi→key").'&steamids='.$stmid64['capt']),true));
            $profile_get[1] = Arrays::get('response→players', json_decode(file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.Arrays::get("steamapi→key").'&steamids='.$stmid64['p2']),true));
            $profile_get[2] = Arrays::get('response→players', json_decode(file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.Arrays::get("steamapi→key").'&steamids='.$stmid64['p3']),true));
            $profile_get[3] = Arrays::get('response→players', json_decode(file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.Arrays::get("steamapi→key").'&steamids='.$stmid64['p4']),true));
            $profile_get[4] = Arrays::get('response→players', json_decode(file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.Arrays::get("steamapi→key").'&steamids='.$stmid64['p5']),true));

            $profile_avatar = [
                'capt' => Arrays::get('0→avatarmedium', $profile_get[0]),
                'p2' => Arrays::get('0→avatarmedium', $profile_get[1]),
                'p3' => Arrays::get('0→avatarmedium', $profile_get[2]),
                'p4' => Arrays::get('0→avatarmedium', $profile_get[3]),
                'p5' => Arrays::get('0→avatarmedium', $profile_get[4]),
            ];

            $profile_status = [
                'capt' => Arrays::get('0→personastate', $profile_get[0]),
                'p2' => Arrays::get('0→personastate', $profile_get[1]),
                'p3' => Arrays::get('0→personastate', $profile_get[2]),
                'p4' => Arrays::get('0→personastate', $profile_get[3]),
                'p5' => Arrays::get('0→personastate', $profile_get[4]),
            ];

            $profile_name = [
                'capt' => escape(Arrays::get('0→personaname', $profile_get[0])),
                'p2' => escape(Arrays::get('0→personaname', $profile_get[1])),
                'p3' => escape(Arrays::get('0→personaname', $profile_get[2])),
                'p4' => escape(Arrays::get('0→personaname', $profile_get[3])),
                'p5' => escape(Arrays::get('0→personaname', $profile_get[4]))
            ];
        }
    }

I hope someone was so patient to read all of this stuff and can help me with making it work :D

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog