?
Solved

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

Posted on 2014-10-08
3
Medium Priority
?
208 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Industry Leaders: 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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

762 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