/** * mixed CSVData(string $db, string $table, array $options = null) * * Returns the content of a table in CSV format * * @param string $db the database name * @param string $table the table name * @param string $what whether to obtain an excel compatible csv format or a simple csv one * @param array $options (optional) (from, to, what = {excel, csv}) * @return mixed false if error occurs, string if ok * @access public * @static * @see DUMP_CRLF */ public static function CSVData($db, $table, $options = null) { $what = isset($options['what']) ? $options['what'] : 'excel'; // Handles the EOL character $crlf = DUMP_CRLF; if ($what == 'excel') { $crlf = "\r\n"; } else { if (get_magic_quotes_gpc()) { $crlf = stripslashes($crlf); } $crlf = str_replace('\\r', "\r", $crlf); $crlf = str_replace('\\n', "\n", $crlf); $crlf = str_replace('\\t', "\t", $crlf); } // end if // Handles the "separator" and the optional "enclosed by" characters $sep = ''; if ($what == 'excel') { $sep = ','; } else { if (get_magic_quotes_gpc()) { $sep = stripslashes($sep); } $sep = str_replace('\\t', "\t", $sep); } $encBy = ''; if ($what == 'excel') { $encBy = '"'; } elseif (get_magic_quotes_gpc()) { $encBy = stripslashes($encBy); } $escBy = ''; if ($what == 'excel' || empty($escBy) && $encBy != '') { // double the "enclosed by" character $escBy = $encBy; } elseif (get_magic_quotes_gpc()) { $escBy = stripslashes($escBy); } // Defines the offsets to use isset($options['to']) && $options['to'] > 0 && (isset($options['from']) && $options['from'] >= 0) ? $limitClause = ' LIMIT ' . ($options['from'] > 0 ? $options['from'] . ', ' : '') . $options['to'] : ($limitClause = ''); $localConn = new DbConnection(); if (!$localConn->connect()) { return false; } // Gets the data from the database $localQuery = 'SELECT * FROM ' . self::backQuote($db) . '.' . self::backQuote($table) . $limitClause; if (!$localConn->exec($localQuery)) { return false; } if ($localConn->numRows() == 0) { return ''; } $numFields = $localConn->numFields(); $fnames = ''; for ($i = 0; $i < $numFields - 1; $i++) { $encBy == '' ? $fnames .= $localConn->fieldName($i) . $sep : ($fnames .= $encBy . str_replace($encBy, $escBy . $encBy, $localConn->fieldName($i)) . $encBy . $sep); } $encBy == '' ? $fnames .= $localConn->fieldName($i) . $crlf : ($fnames .= $encBy . str_replace($encBy, $escBy . $encBy, $localConn->fieldName($i)) . $encBy . $crlf); $buffer = trim($fnames) . $crlf; @set_time_limit(OPEN_EXEC_TIME_LIMIT); // Format the data $i = 0; while ($row = $localConn->fetchRow(MYSQL_NUM)) { $dataTable = ''; for ($j = 0; $j < $numFields; $j++) { if (!isset($row[$j])) { $dataTable .= 'NULL'; } elseif ($row[$j] == '0' || $row[$j] != '') { // always enclose fields if ($what == 'excel') { $row[$j] = preg_replace("/\r(\n)?/", "\n", $row[$j]); } $encBy == '' ? $dataTable .= $row[$j] : ($dataTable .= $encBy . str_replace($encBy, $escBy . $encBy, $row[$j]) . $encBy); } else { $dataTable .= ''; } if ($j < $numFields - 1) { $dataTable .= $sep; } } // end for $buffer .= trim($dataTable) . $crlf; ++$i; } // end while $buffer .= $crlf; $localConn->close(); return $buffer; }