Jump to content

Writing in different columns in an excel file


Goblo

Recommended Posts

Hello there,

 

I'm a student and I've recently started programming PHP. Now I got this exercise where I have to get information from a database into an Excel file. Everything is going well, expect that when the data is written in the Excel file, the data is written in 1 cell.

 

Excel file:

 

excelfileexample.png

Code:

<?php
//connection parameters
$location = "localhost";
$database = "database";
$password = "password";
$username = "username";

//connection info
$con=mysqli_connect($location, $database, $password, $username);
//check if connection was made
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$query = "SELECT * FROM persons";
$result = mysqli_query($con, $query);
$file = "write.csv";
$open = fopen($file, 'w');

while($row = mysqli_fetch_array($result))
{
	$FirstName = $row['FirstName'];
	$LastName = $row['LastName'];
	$Age = $row['Age'];
	$write = "$FirstName, $LastName, $Age\n";
	
	fwrite($open, $write);
}

fclose($open);
?>

The data should be written in seperated cells. In A the Firstname, in B the Lastname and in C the age. I've searched a bit around and only found the tab option (\t) which didn't work.

 

Is it possible to seperate the data and if so, how?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.