虽然循环卡在第一个查询结果上

时间:2016-06-22 01:30:39

标签: php

我对php比较新,我正在转移我写给OOP的搜索引擎。

相关SQL连接代码/ sqlconnect1类:

    public function query($query){
    $this->stmt = $this->dbh->prepare($query);
    }

    public function bind($param, $value, $type = null){     
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }

        $this->stmt->bindValue($param, $value, $type);
    }
    public function execute(){
        return $this->stmt->execute();
    }

    public function resultset(){
        $this->execute();
        return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }

while loop:

$search = new LoadSearch;
$sqlconnect1 = new sqlconnect1;
$sqlconnect1->query("SELECT * FROM data1 WHERE address_city LIKE 'austin' AND address_zip LIKE '78758'");

    //Assign Variables and Display Record Results
    while ($row1 = $sqlconnect1->resultset()){
    $Name1=$row1['name'];
    $Address1=$row1['address_1'];
    $City1=$row1['address_city'];
    $State1=$row1['address_state'];
    $ZIP1=$row1['address_zip'];
    $Country1=$row1['address_country'];
    $Phone1=$row1['phone'];
    $Website1=$row1['website'];
    $Category_11=$row1['category_1'];
    $Category_21=$row1['category_2'];
    echo"

    <!--Table 2-->  

    <table class='table2'>

        <tr>
            <td></td><td><b>$Name1</b></td>
        </tr>

        <tr>
            <td></td><td>$Address1, $City1, $State1 $ZIP1</td>
        </tr>

        <tr>
            <td></td><td>$Country1</td>
        </tr>

        <tr>
            <td></td><td>$Phone1</td>
        </tr>

        <tr>
            <td></td><td>$Category_11</td>
        </tr>

    </table>";

每当加载页面时,它会无限期地重复第一个查询结果,并且永远不会超出第一个结果。任何建议将不胜感激。当程序化时,类似的设置工作正常。

1 个答案:

答案 0 :(得分:3)

删除$this->execute()调用,因为您调用每个循环迭代execute然后调用fetch。通过这种方式,您将始终检索相同的结果(第一行)

public function resultset(){
    // $this->execute(); -- Remove this line
    return $this->stmt->fetch(PDO::FETCH_ASSOC);
}

显然你需要在你的while循环之前调用$sqlconnect1->execute()