Hello all,
I am trying to query an INFORMIX database, and then take the results and
export them into a CSV or Excel file. I saw TONS of examples on how to do
this using MySQL and tried to adpt them for ifx, but I can't get the darn
thing working. My code below sets variables for values passed from my form.
I then connect to my database and run a query that gets results based on the
variable values. After that I want to count the number of fields(columns)
and begin to build the export file.
Well, when I execute the page I get the popup to save/open the output as an
Excel file. When I open it, instead of getting data, it returns an error
message of: PHP Warning: Wrong parameter count for ifx_fieldtypes()
I am trying to use ifx_fieldtypes() to replace the mysql function of
mysql_field_name()
Any ideas?
<?php
$name_code = $_POST['name'];
// echo $name_code;
$case_age = $_POST['case_age'];
// echo $case_age;
$case_cat = $_POST['case_cat'];
// echo $case_cat;
$case_status = $_POST['case_status'];
// echo $case_status;
include 'Conn/prpr_ifx.php';
if (!$connect_id = ifx_connect("[EMAIL PROTECTED]", $user, $pass)) { // THE
ACTUAL CONNECTION
echo "Unable to connect to Informix Database\n"; // DISPLAY IF
CONNECTION FAILS
exit();
}
function parseCSVComments($comments) {
$comments = str_replace('"', '""', $comments); // First off escape all "
and make them ""
if(eregi(",", $comments) or eregi("\n", $comments)) { // Check if I have
any commas or new lines
return '"'.$comments.'"'; // If I have new lines or commas escape them
} else {
return $comments; // If no new lines or commas just return the value
}
}
$sql = "SELECT * FROM pending_summary_detail WHERE name = '$name_code'";
if (!empty($case_age)) {
$sql.=" AND case_age_group = '$case_age'";
}
if (!empty($case_cat)) {
$sql.=" AND case_category = '$case_cat'";
}
if (!empty($case_status)) {
$sql.=" AND case_status = '$case_status'";
} // Start our query of the database
$query = ifx_query ($sql, $connect_id);
$numberFields = ifx_num_fields($query); // Find out how many fields we are
fetching
if($numberFields) { // Check if we need to output anything
for($i=0; $i<$numberFields; $i++) {
$head[] = ifx_fieldtypes($query, $i); // Create the headers for each
column, this is the field name in the database
}
$headers = join(',', $head)."\n"; // Make our first row in the CSV
while($info = ifx_fetch_row($query)) {
foreach($head as $fieldName) { // Loop through the array of headers as we
fetch the data
$row[] = parseCSVComments($info->$fieldName);
} // End loop
$data .= join(',', $row)."\n"; // Create a new row of data and append it
to the last row
$row = ''; // Clear the contents of the $row variable to start a new row
}
// Start our output of the CSV
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=data.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $headers.$data;
} else {
// Nothing needed to be output. Put an error message here or something.
echo 'No data available for this CSV.';
}
?>