mysqli query only returning first row

前端 未结 5 2100
不思量自难忘°
不思量自难忘° 2020-12-12 03:16

I am migrating from mysql to mysqli, and I am having trouble returning more than one row from the database in a query.

$db = new mysqli($hostname, $sql_us, $         


        
5条回答
  •  醉酒成梦
    2020-12-12 03:47

    Use this code:

    $rows = array();
    if ($type == 'assoc') {
        while($row = $result->fetch_assoc()) {
          $rows[] = $row;
        }
    } else {    
        while($row = $result->fetch_object()) {
          $rows[] = $row;
        }   
    }
    return $rows;
    

    You are using the return inside the while and return terminates the while loop after first iteration that's why you are getting only one row.

提交回复
热议问题