为什么从php类访问mysql时while循环会产生无限结果

时间:2018-11-28 08:15:45

标签: php class mysqli

有一个如下定义的类

class table {

    public $mysqli;
    public $display;

    public function mysqli() {
         $mysqli = new mysqli('hostname', 'username', 'password', 'database');
         return $mysqli;
    }

    public function __construct() {
         $this->mysqli = $this->mysqli();
    }

    public function display() {

         $sql = "SELECT * FROM table";
         if($data = $this->mysqli->query($sql)) {
               $this->display = $data->fetch_assoc();
         };
    }
}

$table = new table();
$table->display();
while($table->display) {
    $this->display['name']. " " . $this->display['country'];
};

为公共功能$this->display分配了值$data->fetch_assoc()。间接地,while($table->display = $data->fetch_assoc())等效于while($data->fetch_assoc()。但结果不会遍历列表。相反,它只会一遍又一遍地显示第一行,直到系统崩溃为止。

是否存在我不了解的指导php类的规则,或者我错过了命令?

2 个答案:

答案 0 :(得分:1)

您不应该像这样使循环循环吗:

while($row = $table->display) {
    $row['name']. " " . $row['country'];
};

答案 1 :(得分:0)

尝试:

TreeMap<Integer, List<String>> res = list.stream()
    .collect(groupingBy(
        s -> s.charAt(0) % 3,
        x -> new TreeMap<>(Comparator.reverseOrder()),
        Collectors.toList()
    ));