connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; exit; } $fp = fopen($filename, 'w'); fputcsv($fp, $header, $delimiter, $enclosure); if (!$result = $mysqli->query($sql)) { echo $mysqli->error; exit; } if ($result->num_rows > 0) { $rTotal = $result->num_rows; $r = 0; while ($row = $result->fetch_object()) { $r++; $fline = array(); foreach($header as $h) { $fline[] = $row->$h; } fputcsv($fp, $fline, $delimiter, $enclosure); echo str_pad($r, strlen($rTotal), ' ', STR_PAD_LEFT)." / ".$rTotal."\n"; } $result->close(); } fclose($fp);