Solved

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

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now