user5236938
user5236938

Reputation:

Writing to file from sql rows

I'm attempting to write to a file from a function that returns several SQL rows. The issue I'm having is I only get one row back with return and 0 rows back if I use echo or printf

This is from cron.php

<?php
require 'lib/app.php';
//All pilots
$str = $mining->activePilots(0);
$file = fopen('elements/pilots.html',"w");
fwrite($file,$str);
fclose($file);

This is from the method:

while($row = mysqli_fetch_array($sql)) 
{ 
     $pilot = $row['username'];
     $ship = $row['ship'];
     $role = $user->opRole($row['role']);
     $hours = $row['hours'];
   return "<tr><td>$role</td><td>$pilot</td><td>$ship</td><td>$hours</td></tr>";
 }

Upvotes: 2

Views: 32

Answers (1)

Jo&#227;o Pedro
Jo&#227;o Pedro

Reputation: 546

Return ends the execution of your program and passes the value obtained in the first loop. You should store the different values inside a variable and then in the end return all:

$store='';

    while($row = mysqli_fetch_array($sql)) 
    { 
         $pilot = $row['username'];
         $ship = $row['ship'];
         $role = $user->opRole($row['role']);
         $hours = $row['hours'];
       $store=$store."<tr><td>$role</td><td>$pilot</td><td>$ship</td><td>$hours</td></tr>";
     }
return $store;

Upvotes: 1

Related Questions