0

What I am having trouble with is creating an CSV file from an associative array in using PHP. My associative array is the result of a sqlsrv_fetch_array function.

When I run the code:

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  echo '<pre>';
  print_r($row);
  echo '</pre>';
}

I can see:

Array
  (
  [Username] => User1
  [Password] => Password1
  [DisplayName] => UserOne
  [FirstName] => User
  [LastName] => One
  [Role] => Bacon
  [HomeEmailAddress] => [email protected]
)
Array
(
  [Username] => User2
  [Password] => Password2
  [DisplayName] => UserTwo
  [FirstName] => User
  [LastName] => Two
  [Role] => Egg
  [HomeEmailAddress] => [email protected]
)
Array
(
  [Username] => User3
  [Password] => Pasword3
  [DisplayName] => UserThree
  [FirstName] => User
  [LastName] => Three
  [Role] => Sausage
  [HomeEmailAddress] => [email protected]
)

All looking good. However the problem I am having is getting that into a CSV file. I want the file to look like

Username,Password,DisplayName,FirstName,LastName,Role,HomeEmailAddress
User1,Password1,UserOne,User,One,Bacon,[email protected]
User2,Password2,UserTwo,User,Two,Egg,[email protected]
User3,Password3,UserThree,User,Three,Sauage,[email protected]

I've been banging my head trying to get fputcsv working, using a foreach ($row as $key => $value) loop inside the while loop, but I am getting nowhere fast. Is this a futile quest? Can this be done on an array created using _sql_fetch_array_.

If I put the code in my where loop:

$fp = fopen('file.csv', 'w');

foreach ($row as $key => $value) {
 fputcsv($fp, $row);
}

fclose($fp);

I get a csv file with the output:

User3,Password3,UserThree,User,Three,Sauage,[email protected]
User3,Password3,UserThree,User,Three,Sauage,[email protected]
User3,Password3,UserThree,User,Three,Sauage,[email protected]
User3,Password3,UserThree,User,Three,Sauage,[email protected]
User3,Password3,UserThree,User,Three,Sauage,[email protected]
User3,Password3,UserThree,User,Three,Sauage,[email protected]
User3,Password3,UserThree,User,Three,Sauage,[email protected]

Any hints or suggestions would be greatly appreciated.

4
  • I'm afraid can't understand the rationale behind the expected output. What's the separator, an indeterminate number of white spaces starting from two? What if data itself contains spaces? Are you sure this is CSV at all, and not some fixed width file format? Plus it's strange you think the database code is more relevant than your use of fputcsv(). Commented Nov 19, 2017 at 14:47
  • Might be a poor formed question because I am not understanding something in my head. I'm pretty inexperienced with this stuff. Commented Nov 19, 2017 at 14:56
  • You're getting correct CSV. The file format you want does not look like CSV. What exact specs have you been given? Commented Nov 19, 2017 at 15:05
  • Sorry, you're correct. I have edited my original question so my desired result is proper csv. Thanks for pointing that out, my bad. what I'm edited is how I would like it to look. Commented Nov 19, 2017 at 15:11

1 Answer 1

0

You need to combine the two sets of code you have ( i.e. read each row and then write the row out to your CSV.

$fp = fopen('file.csv', 'w');
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
 fputcsv($fp, $row);

}

fclose($fp);

The way you were doing it was just looking at a single row.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! From my reading I thought I had to use a foreach loop. I can just hardcode in the headings, though a solution that also pulled in the headings would make it more flexible in the future. However, thank you!

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.