Web Hosting Talk







View Full Version : PHP Array Issue


dougp25
09-09-2007, 10:11 AM
Hi All.

I am using a script from the PHP Cookbook by O'Reilly, but having some issues. Basically when I run the script, I get this error in the browser window:

Warning: fputcsv() expects parameter 2 to be array

When I paste my query in phpMyAdmin, I get back roughly 100 rows of data, so I know the query is good. I guess I need to somehow convert the query to an array?? Here is my code so far (the query is long and nested and ugly, so most of it can be ignored!) (I should mention that I use a class called ez_sql to perform my queries, but I'm not against doing is some other way!)

$sSQL="SELECT studentbio.studentbio_id,
studentbio.studentbio_lname, studentbio.studentbio_fname,
school_names.school_names_desc, grades.grades_desc FROM ((studentbio
INNER
JOIN student_grade_year ON studentbio.studentbio_id =
student_grade_year.student_grade_year_student) INNER JOIN school_names
ON
studentbio.studentbio_school = school_names.school_names_id) INNER JOIN
grades ON student_grade_year.student_grade_year_grade = grades.grades_id
ORDER BY
studentbio.studentbio_lname";

$student_data=$db->get_results($sSQL);
$output=fopen('php://output','w') or die("Can't open php://output");

//Print header row
fputcsv($output,array('StudentID','LastName','FirstName','School','Grade'));
foreach($student_data as $student_line) {
fputcsv($output, $student_line);
$total += $student_line[3];
}
?>

Thanks for any help!

bqinternet
09-09-2007, 11:15 AM
Have you verified that $student_data really is an array of arrays? I would throw in print_r($student_data) call before your foreach statement so that you can see exactly what you're dealing with.

eanixlive
09-09-2007, 09:59 PM
Try implementing the "is_array()" function to make sure the data is actually an array, if it is not you may need to handle it differently or convert it to an array