php - mysqli_fetch_assoc dies on first row -
i have following table in mysql:
╔══════╦════╗
║... city.....║...zip..║
╠══════╬════╣
║ prague. ║ 11000║
║ brno..... ║ 34785║
╚═══════════╝
the following code works:
class mysql{ function connect(){ $this->c = new mysqli( $this->option['dbhost'], $this->option['dbuser'], $this->option['dbpass'], $this->option['dbname'] ); if($this->c->connect_errno){ exit("connect failed: ".$this->c->connect_error); } if(!$this->c->select_db($this->option['dbname'])){ exit("cannot select database: ".$this->option['dbname']); } } function query($query){ if(!$this->c->query($query)){ exit("cannot execute query: {$query}<br>mysql error: {$this->c->error}<br>mysql error code: {$this->c->errno}"); } return $this->c->query($query); } function fetch_assoc($result){ return $result->fetch_assoc(); } } $db = new mysql; $db->connect(); $query = $db->query("select city, zip cities"); while($row = $db->fetch_assoc()){ print_r($row); }
the code works until try change fetch_assoc part this:
function fetch_assoc($query){ $result = $this->query($query); return $result->fetch_assoc(); } } $db = new mysql; $db->connect(); while($row = $db->fetch_assoc("select city, zip cities")){ print_r($row); }
in case endlessly (stopped max execution time) prints first row of table: array ( [city] => prague [zip] => 11000 )
i believe should it, making query once , iterating on mentioned in comments above:
$db = new mysql; $db->connect(); $query = $db->query("select city, zip cities"); $result = $db->fetch_assoc(); while ($row = $result->fetch_assoc()) print_r($row);
Comments
Post a Comment