Solved

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

Posted on 2014-10-08
3
149 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
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.

737 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