Solved

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

Posted on 2014-10-08
3
139 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
3 Comments
 
LVL 109

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 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