Goblo Posted May 23, 2013 Share Posted May 23, 2013 (edited) 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: 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 May 23, 2013 by Goblo Quote Link to comment https://forums.phpfreaks.com/topic/278315-writing-in-different-columns-in-an-excel-file/ Share on other sites More sharing options...
davidannis Posted May 23, 2013 Share Posted May 23, 2013 Try enclosing the values in quotes: $write = "\"$FirstName\", \"$LastName\", \"$Age\"\n"; Quote Link to comment https://forums.phpfreaks.com/topic/278315-writing-in-different-columns-in-an-excel-file/#findComment-1431857 Share on other sites More sharing options...
requinix Posted May 23, 2013 Share Posted May 23, 2013 fputcsv is your friend. Quote Link to comment https://forums.phpfreaks.com/topic/278315-writing-in-different-columns-in-an-excel-file/#findComment-1431892 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.