1

I want to put data in array which then put in excel file but it does not work.

    $sql="SELECT `Jobc_id`, `Customer_name`, `Veh_reg_no`, `MSI_cat`, `Mileage` FROM `jobcard`";
$result = $conn->query($sql);

while($row = $result->fetch_assoc()){

    array( $row["job_id"],$row["Customer_name"],$row["Veh_reg_no"],$row["MSI_cat"],$row["Mileage"]);
}
foreach($rows as $row)
    $writer->writeSheetRow('Sheet1', $row);

Whereas, below code work..

$rows = array(
    array('2003','1','-50.5','2010-01-01 23:00:00','2012-12-31 23:00:00'),
    array('2003','B1', '23.5','2010-01-01 00:00:00','2012-12-31 00:00:00'),
); 
 foreach($rows as $row)
        $writer->writeSheetRow('Sheet1', $row);

How can I make first code to work :( pls help

3
  • You never assign a name to the array Commented Feb 9, 2018 at 15:35
  • 1
    ^^ Or rather you never assign the array to a variable. Commented Feb 9, 2018 at 15:36
  • "it does not work" We get this a lot. Do you get any errors? Where do you set $rows in your first code? Commented Feb 9, 2018 at 15:37

1 Answer 1

2

you are not assigning the array to a variable, therefore you can't access your fetched data after the while loop. This should fix it:

$rows = [];
while($row = $result->fetch_assoc()){
    $rows[] = [$row["job_id"],$row["Customer_name"],$row["Veh_reg_no"],$row["MSI_cat"],$row["Mileage"]];
}
// now you can use $rows
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.