I am having problem with special character e.g. Russian characters are converted into ???? when I export MySQL query to CSV file.I used iconv() function but it is not working.
What should I do to fix this issue. I read different posts but nothing seems to be working for me
public function generateReport($nameReport,$db,$query,$host,$user,$pwd){
$con = mysql_connect($host,$user,$pwd);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db($db, $con);
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysql_error());
}
$res = mysql_query($query);
if (!$res) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
$file='/home/'.$nameReport.'.csv';
$fp = fopen($file,'w') or exit("Unable to open file!");
if($fp){
$row = mysql_fetch_assoc($res);
$line = "";
$comma = "";
foreach($row as $name => $value) {
$line .= $comma . '"' . str_replace('"', '""', $name) . '"';
$comma = ",";
}
$line .= "\n";
fputs($fp, $line);
mysql_data_seek($res, 0);
while($row = mysql_fetch_assoc($res)) {
$line = "";
$comma = "";
foreach($row as $value) {
iconv( mb_detect_encoding( $value ), 'UTF-8', $value);
$line .= $comma . '"' . str_replace('"', '""', $value) . '"';
$comma = ",";
}
$line .= "\n";
fputs($fp, $line);
}
}
return '/home/'.$nameReport.'.csv';
}
Why not export directly from MySQL to CSV without going through PHP?
SELECT field_you_want, other_field_you_want
FROM your_table
INTO OUTFILE 'c:\\filename.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
for russian only, you can try this
$value = mb_convert_encoding($value, 'KOI8-R','UTF-8' );
$line .= $comma . '"' . str_replace('"', '""', $value) . '"';
$comma = ",";
for other charsets, you have to find a way to get the charset info.
maybe, mb_detect_encoding with mb_detect_order
or maybe if you get that text from a website: from server header or charset defined at the head of page...
Related
I am trying to write a backup for my database,
I want If a button is clicked the application will export the database to the desktop.
The app exports the file on my database directory if I use laravel database directory path
but if I change the path to desktop location nothing appears on the desktop.
$get_all_table_query = "SHOW TABLES";
$result = DB::select(DB::raw($get_all_table_query));
$tables = [
'users',
'migrations'
];
$structure = '';
$data = '';
foreach ($tables as $table) {
$show_table_query = "SHOW CREATE TABLE " . $table . "";
$show_table_result = DB::select(DB::raw($show_table_query));
foreach ($show_table_result as $show_table_row) {
$show_table_row = (array)$show_table_row;
$structure .= "\n\n" . $show_table_row["Create Table"] . ";\n\n";
}
$select_query = "SELECT * FROM " . $table;
$records = DB::select(DB::raw($select_query));
foreach ($records as $record) {
$record = (array)$record;
$table_column_array = array_keys($record);
foreach ($table_column_array as $key => $name) {
$table_column_array[$key] = '`' . $table_column_array[$key] . '`';
}
$table_value_array = array_values($record);
$data .= "\nINSERT INTO $table (";
$data .= "" . implode(", ", $table_column_array) . ") VALUES \n";
foreach($table_value_array as $key => $record_column)
$table_value_array[$key] = addslashes($record_column);
$data .= "('" . wordwrap(implode("','", $table_value_array),400,"\n",TRUE) . "');\n";
}
}
$file_name = dirname("C:\Users\ADC-2\Desktop") .'/database_backup_on_' . date('y_m_d') . '.sql';
$file_handle = fopen($file_name, 'w + ');
$output = $structure . $data;
fwrite($file_handle, $output);
fclose($file_handle);
echo "DB backup ready";
It will display DB backup ready but nothing really shows up on the desktop.
dirname()
Use
"C:\\Users\\ADC-2\\Desktop"
instead of
"C:\Users\ADC-2\Desktop"
I have been looking for ways to export my data to an Excel spreadsheet. I have tried loads of examples and have been unsuccessful. This is my current code that returns, "Query Failed!". when changing the "ORDER BY" however, the spreadsheet returns the data but loops an error underneath. This is the code ive found and am using:
$con = mysql_connect($hostname,$username,$password,$db_name) or die('Could not connect: ' . mysql_error());
function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
$filename = "website_data_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-type: application/vnd.ms-excel; charset=UTF-16LE");
$flag = false;
$result = mysql_query("SELECT * FROM test ORDER BY field") or die('Query failed!');
while(false !== ($row = mysql_fetch_assoc($result)))
{
if(!$flag)
{
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
array_walk($row, 'cleanData');
echo implode("\t", array_values($row)) . "\r\n";
}
Can anyone tell me why this happens?
(The same thing happens using mysqli functions too)
I would like to know, I have just created my datatables, but two of them are giving JSON formatting errors. I am doing a join of two tables in these two. I tried running the query in phpmyadmin, and it works just fine Here is one example of my server-side files:
<?php
$username="drup197";
$password="*****";
$database="census";
$server="localhost";
$link = mysqli_connect($server,$username,$password,$database);
//#mysql_select_db($database,$link) or die( "Unable to select database");
$query = "
SELECT *
FROM national_age_gender_demographics INNER JOIN arizona_age_gender_demogrpahics
WHERE national_age_gender_demographics.age_group = arizona_age_gender_demogrpahics.age_group
ORDER BY national_age_gender_demographics.index_number";
$result = mysqli_query($link,$query);
if(!$result) die( "Query: " . $query . "\nError:" . mysql_error() );
//print_r($row);
$tableData = '{"aaData": [[';
$numRows = $result->num_rows;
$row = mysqli_fetch_array($result);
for ($i = 0; $i < $numRows; $i++) {
if ($i != 0) {
$tableData .= ",[";
}
$tableData .= '"' . $row['age_group'] . '",';
$tableData .= '"' . $row['national_age_gender_demographics.both_pop'] . '",';
$tableData .= '"' . $row['national_age_gender_demographics.male_pop'] . '",';
$tableData .= '"' . $row['national_age_gender_demographics.female_pop'] . '",';
$tableData .= '"' . $row['national_age_gender_demographics.male_percent'] . '",';
$tableData .= '"' . $row['national_age_gender_demographics.female_percent'] . '",';
$tableData .= '"' . $row['national_age_gender_demographics.both_percent'] . '",';
$tableData .= '"' . $row['national_age_gender_demographics.males_per_100_females'] . '",';
$tableData .= '"' . $row['arizona_age_gender_demographics.both_pop'] . '",';
$tableData .= '"' . $row['arizona_age_gender_demographics.male_pop'] . '",';
$tableData .= '"' . $row['arizona_age_gender_demographics.female_pop'] . '",';
$tableData .= '"' . $row['arizona_age_gender_demographics.male_percent'] . '",';
$tableData .= '"' . $row['arizona_age_gender_demographics.female_percent'] . '",';
$tableData .= '"' . $row['arizona_age_gender_demographics.both_percent'] . '",';
$tableData .= '"' . $row['arizona_age_gender_demographics.males_per_100_females'] . '"]';
if ($i != $numRows - 1) {
$row = mysqli_fetch_array($result);
}
}
$tableData .= ']}';
echo $tableData;
?>
Does anyone know what is wrong here?
Firstly Steven is right, its better to use the json encode (or at least neater) to create your json, save you from the messy ifs and bracket business.
I would also recommend using mysqli_fetch_assoc rather than using mysqli_fetch_array, which as you currently specified should return both associative and numbered results (see here http://php.net/manual/en/mysqli-result.fetch-array.php), which probably messes up your results.
eg:
...
$row = mysqli_fetch_assoc($result);
$json_data = json_encode($row);
echo $json_data;
?>
Try that and see how you get on?
Do you have an idea how to pull data from mysql, put it in an array then feed it in an autocomplete field?
I have tried hardcoded the values but what I'm thinking is when I add a new record, I have to re-code again the array. I'm a newbie in PHP so I beg your pardon.
Kindly check what I've tried so far:
protected function jsGenerateResourcesAutocomplete(){
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT employee_name FROM employee" ;
mysql_select_db('test', $conn);
$retval = mysql_query($sql);
$row = mysql_fetch_assoc($retval);
if(!$retval )
{
die('Could not select data: ' . mysql_error());
}
$employeeNames = $this->employeeNames;
$html = ""; $html .= 'var employeenames = [' . PHP_EOL;
foreach ($employeeNames as $employeeName) {
$html .= '"' . $employeeName-> $array . '",' . PHP_EOL;
}
$html .= '];';
$html .= '$(".resource-input input").autocomplete({source: employeenames});' .PHP_EOL;
$html .= '});' . PHP_EOL;
$html .= '</script>' . PHP_EOL;
return $html;
}
I believe something in this line:
$html .= '"' . $employeeName-> $array . '",' . PHP_EOL;
I have to put forth the array but I have no idea how to do it. Any help is truly appreciated. Thanks.
Try this way:
$sql = "SELECT employee_name FROM employee" ;
mysql_select_db('test', $conn);
$retval = mysql_query($sql);
if(!$retval )
{
die('Could not select data: ' . mysql_error());
}
$data = array();
while($row = mysql_fetch_array($retval)){
$data[] = $row['employee_name'];
}
$html = "<script>";
$html .= 'var employeenames = '.json_encode($data);
$html .= '$(".resource-input input").autocomplete({source: employeenames});';
$html .= '});';
$html .= '</script>';
return $html;
Now i have such method:
function exportFromTransbase($table_name) {
$odbc_query = "SELECT * FROM " . $table_name;
$data = odbc_exec($this->odbc_id, $odbc_query);
odbc_longreadlen($data, 10485760);
while($row = odbc_fetch_array($data))
{
foreach($row as $key => $value) {
$keys[] = "`" . $key . "`";
$values[] = "'" . mysql_real_escape_string($value) . "'";
}
$mysql_query = "INSERT INTO `" . strtolower(substr($table_name, 4)) . "` (" . implode(",", $keys) . ") VALUES (" . implode(",", $values) . ")";
mysql_query($mysql_query);
set_time_limit(3600);
unset($keys);
unset($values);
unset($row);
}
if ($mysql_query){
print "Ýêñïîðò äàííûõ èç òàáëèöû " . $table_name . " çàâåðøåí!";
//strtolower(substr($table_name, 4))
}
}
But it's very slow when importing to mysql. I decide to change this to export to file .sql, so that in future i can via terminal or phpmyadmin import that table. How to change to export to sql file my data?
Note! i'm converting from transbase to mysql
Instead of...
while($row = odbc_fetch_array($data))
{
foreach($row as $key => $value) {
$keys[] = "`" . $key . "`";
$values[] = "'" . mysql_real_escape_string($value) . "'";
}
$mysql_query = "INSERT INTO `" . strtolower(substr($table_name, 4)) . "` (" . implode(",", $keys) . ") VALUES (" . implode(",", $values) . ")";
mysql_query($mysql_query);
set_time_limit(3600); // this should not be here
unset($keys); // this is redundant
unset($values); // and this
unset($row); // and this too
}
Try:
$oufile=fopen("export.sql", 'w') || die("error writing file");
while($row = odbc_fetch_array($data))
{
foreach($row as $key => $value) {
$keys[] = "`" . $key . "`";
$values[] = "'" . mysql_real_escape_string($value) . "'";
}
$mysql_query = "INSERT INTO `" . strtolower(substr($table_name, 4)) . "` (" . implode(",", $keys) . ") VALUES (" . implode(",", $values) . ")";
fputs($outfile, $mysql_query . ";\n";
}
However it'll be much faster if you....
$oufile=fopen("export.sql", 'w') || die("error writing file");
fputs($outfile, "ALTER TABLE `" . . strtolower(substr($table_name, 4)) . "` DISABLE KEYS;\n";
while($row = odbc_fetch_array($data))
{
foreach($row as $key => $value) {
$keys[] = "`" . $key . "`";
$values[] = "'" . mysql_real_escape_string($value) . "'";
}
$head="INSERT DELAYED INTO `" . strtolower(substr($table_name, 4)) . "` (" . implode(",", $keys) . ") VALUES ";
$row[]="(" . implode(",", $values) . ")";
if (count($row)>100) {
flush_ins($outfile, $head, $row);
$row=array();
}
}
if (count($row)) flush_ins($outfile, $head, $row);
fputs($outfile, "ALTER TABLE `" . . strtolower(substr($table_name, 4)) . "` ENABLE KEYS;\n";
fclose($outfile);
...
function flush($rows, $head, $fh)
{
fputs($fh, $head . implode("\n,", $rows) . ";\n");
}
see this post:
Easy way to export a SQL table without access to the server or phpMyADMIN
it uses the select into outfile syntax. The docs for this syntax are here: http://dev.mysql.com/doc/refman/5.1/en/select-into.html
you would do something like this:
mysql_query('SELECT * INTO OUTFILE "/path/to/my_file/my_file.sql" from '.$table_name)
Then this .sql file will be on your server.
If you don't have permissions to run the select into outfile synatx. You can use the mysqldump utility, like this:
<?php
exec('mysqldump db_name '.$table_name.' > my_file.sql');
?>
This will create an .sql file with the name indicated.