FETCH_ASSOC PDO连续循环

时间:2017-04-15 17:58:38

标签: php sql pdo while-loop

我是PDO的新手,由于我最近在网络空间遇到的问题,我正在从MySQLi转换到它。

我正在尝试使用FETCH_ASSOC来循环遍历结果以获取我的查询生成的各个记录,但是,循环永远不会结束并且一遍又一遍地连续重复结果中的第一条记录。有什么建议吗?

<?php
$Request = databaseManager::getDB()->prepareSQL("SELECT Username, 2Wins, 2Loses, (2Wins/(2Wins+2Loses))*100 as WinRate FROM tblUsers ORDER BY 4 DESC"); 
//$LstResults = $Request->fetchAll();

while($LstResults = $Request->fetchArray()){
    if($LstResults["WinRate"] >= 50){ ?>
        <tr>
        <td><?php echo $LstResults["Username"]; ?></td>                                 
        <td>
            <div class="progress progress-xs">
                <div class="progress-bar progress-bar-success" style="width: <?php echo $LstResults["WinRate"]; ?>%"></div>
            </div>
        </td>
        <td><span class="badge bg-green"><?php echo intval($LstResults["WinRate"]); ?></span></td>
        </tr>
    <?php } 
} ?>

class databaseManager{
    protected $dbHND;
    protected $currentSQLStatement;
    protected $bind;

    private $StrDBHost='localhost';
    private $StrDBPort = "3306";
    private $StrDBUser='thomassm_sqlogin';
    private $StrDBPass='password';
    private $StrDBName='thomassm_CadetPortal';

    public function __construct(){
        if ($this->dbHND = new PDO ('mysql:host=' . $this->StrDBHost . ';dbname=' . $this->StrDBName . ';port=' . $this->StrDBPort, $this->StrDBUser, $this->StrDBPass)){
            $this->bind = null;
        }
        else{
            return false;
        }
    }

    static public function getDB(){
        $tmpInstance = new self ();
        return $tmpInstance;
    }

    public function prepareSQL($sqlQueryString = null){
        if ($sqlQueryString === null){
            $this->currentSQLStatement === false;
            return false;
        }
        elseif (is_array ( $sqlQueryString )){
            if ($this->currentSQLStatement = $this->dbHND->prepare ( $sqlQueryString [0] )){
                $this->bind = $sqlQueryString [1];
                return $this;
            }
            else{
                throw new \Exception ( "Could not prepare SQL Statement, " . print_r ( $this->dbHND->errorInfo () ) );
                return false;
            }
        }
        else{
            if ($this->currentSQLStatement = $this->dbHND->prepare ( $sqlQueryString )){
                return $this;
            }
            else{
                throw new \Exception ( "Could not prepare SQL Statement, " . print_r ( $this->dbHND->errorInfo () ) );
                return false;
            }
        }
    }

    public function fetchArray(){
        if (! $this->currentSQLStatement){
            return false;
        }
        elseif ($this->bind != null){
            $this->currentSQLStatement->execute ( $this->bind );
            return $this->currentSQLStatement->fetch ( PDO::FETCH_ASSOC );
        }
        else{
            $this->currentSQLStatement->execute ();
            return $this->currentSQLStatement->fetch ( PDO::FETCH_ASSOC );
        }
    }

public function fetchAll(){
    if (! $this->currentSQLStatement){
        return false;
    }
    elseif ($this->bind != null){
        $this->currentSQLStatement->execute ( $this->bind );
        return $this->currentSQLStatement->fetchAll ( PDO::FETCH_ASSOC );
    }
    else{
        $this->currentSQLStatement->execute ();
        return $this->currentSQLStatement->fetchAll ( PDO::FETCH_ASSOC );
    }
}
}

1 个答案:

答案 0 :(得分:0)

正如KIKO软件在对问题的评论中所说,我只需要执行一次执行,否则它将重置循环计数。我已经从fetchArray代码中删除了执行,现在在调用fetchArray之前调用它,如下所示:

public function fetchArray(){
    if (! $this->currentSQLStatement){
        return false;
    }
    else{
        return $this->currentSQLStatement->fetch ( PDO::FETCH_ASSOC );
    }
}