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?

Edited by Goblo
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.