Solved

What's wrong with this code I'm using to create a csv file?

Posted on 2014-10-08
3
167 Views
Last Modified: 2014-10-08
Here's my code:

$don="SELECT * FROM verizon_data where posted_day='2013-08-06' limit 400000";
$don_query=mysqli_query($cxn, $don);
	if(!$don_query)
	{
	$nuts=mysqli_errno($cxn).': '.mysqli_error($cxn);
	die($nuts);
	}
$don_count=mysqli_num_rows($don_query);
echo $don_count; 

$row=mysqli_fetch_array($don_query, MYSQLI_ASSOC);
	$fp = fopen('file.csv', 'w');

		foreach ($row as $val) {
			fputcsv($fp, $val);
		}

		fclose($fp);

Open in new window


The error I'm getting says "Warning: fputcsv() expects parameter 2 to be array, string given in C:\wamp\www\json\sandbox.php on line 20"

Line 20 is actually fputcsv($fp, $val)

$val, I'm assuing is the $row and, my first thought was that there was no data. Nope. Plenty of data, so I'm blowing it somewhere but don't know where.

What am I doing wrong?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40368556
With MySQLi, we use a while() iterator to retrieve the rows of the results set.  Each row can be retrieved in the form of an array, suitable for use with fputcsv().

The correct design pattern is in the code snippet here, at line #144
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28533249.html#a40368499
0
 

Author Comment

by:brucegust
ID: 40368664
Got it working, Ray!

Thanks!

BTW: Some low hanging fruit for if you're willing: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28533765.html

Thanks, again!
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 40368677
Maybe you need to fputcsv (without a loop)
fputcsv($fp, $row);

Open in new window


... edit: too late :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question