php - How to fetch an array from a prepared statement inside a class -
i wrote code:
public function getdataasarray($myquery){ $this->connection = mysqli_connect($this->host, $this->dbusername, $this->dbpassword, 'portal'); $statement = $this->connection->prepare($myquery); $statement->execute(); $data = $statement->get_result()->fetch_array(); $results = array(); while($line = $data){ $results[] = $line; } return $results; }
i'm trying return result database array can use in foreach loop. doesn't work before of exhausted memory allocator.
i have working (unsafe) code:
public function getdataasarray($myquery){ $this->connection = mysqli_connect($this->host, $this->dbusername, $this->dbpassword, 'portal'); $query = mysqli_query($this->connection, $myquery); $results = array(); while($line = mysqli_fetch_array($query)){ $results[] = $line; } return $results; }
could me fix problem?
replace:
while($line = $data){
with:
$result = $statement->get_result(); while($line = $result->fetch_array()) {
in first scenario, $data
variable, not function call. $line = $data
point first row fetch, , run "forever" until allocate available memory.
wiki
Comments
Post a Comment