【问题标题】:FETCH_ASSOC PDO Continuously LoopingFETCH_ASSOC PDO 连续循环
【发布时间】:2017-09-11 18:28:21
【问题描述】:

我是 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 );
    }
}
}

【问题讨论】:

  • 在每次获取之前执行,所以每次都从头开始。
  • @KIKOSoftware 我应该从课堂上删除执行调用然后总是在我以前使用过的地方转到...-&gt;execute()-&gt;fetchArray();吗?
  • 只需要执行一次查询,就可以获取所有的行。我会将执行放入您的prepareSQL() 方法中,然后调用该方法prepareAndExecuteSQL()

标签: php sql pdo while-loop


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-27
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多