Page 1 of 1

Syntax error

Posted: Mon Mar 26, 2012 3:17 am
by krasi_e_d
Hello, guys

I have a problem with charset. When I start my code from browser the data in my table is ?????????, but when I paste the output file in phpadmin everything is ok.
Please, guys help

this is my code:

Code: Select all

<html>
<head>


</head>
<body>
<?php
header("Content-Type: text/xml; charset=cp1251_general_ci"); 
$databasehost = "localhost";
$databasename = "export_ajur";
$databasetable = "invoice";
$databaseusername ="root";
$databasepassword = "password";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "1.csv";

$addauto = 0;


	

$save = 1;
$outputfile = "output.sql";
if(!file_exists($csvfile)) {
	echo "File not found. Make sure you specified the correct path.\n";
	exit;
}
$file = fopen($csvfile,"r");

if(!$file) {
	echo "Error opening data file.\n";
	exit;
}

$size = filesize($csvfile);

if(!$size) {
	echo "File is empty.\n";
	exit;
}

$csvcontent = fread($file,$size);

fclose($file);

$con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());

@mysql_select_db($databasename) or die(mysql_error());
	
	
	
	
	
$lines = 0;
$queries = "";
$linearray = array();

foreach(explode($lineseparator,$csvcontent) as $line) {

	$lines++;

	$line = trim($line," \t");
	
	$line = str_replace("\r","",$line);
	
		
	


$linearray = explode($fieldseparator,$line);
   
   $linemysql = implode("','",$linearray);
   
   if($addauto)
      
      $query = "insert into $databasetable values('$linemysql');";
   else
      $query = "insert into $databasetable values ('$linemysql');";
   
   $queries .= $query . "\n";

   @mysql_query($query);
}
mysql_query("set names 'cp1251_general_ci'",$con);
	mysql_query("SET character_set_client=cp1251_general_ci", $con);
	mysql_query("SET character_set_connection=cp1251_general_ci", $con);
	mysql_query("SET character_set_results=cp1251_general_ci", $con);
@mysql_close($con);

if($save) {
	
	if(!is_writable($outputfile)) {
		echo "File is not writable, check permissions.\n";
	}
	
	else {
		$file2 = fopen($outputfile,"w");
		
		if(!$file2) {
			echo "Error writing to the output file.\n";
		}
		else {
			fwrite($file2,$queries);
			fclose($file2);
		}
	}
	
}

echo "Found a total of $lines records in this csv file.\n";


?>
</body>
</html>

Re: Syntax error

Posted: Mon Mar 26, 2012 3:41 am
by krasi_e_d
When I paste this output. Everything is ok.
[text]
insert into invoice values ('ID_PURCHASE_INVOICE','DOC_TYPE_NAME','DOC_EXT_CODE','DOC_NUM','DOC_DATE','WOVAT_AMOUNT','VAT_AMOUNT','TOTAL_AMOUNT','CONTRAGENT_NAME','CONTRAGENT_BULSTAT','CONTRAGENT_VATNUMBER','CONTRAGENT_POST_ADDRESS','CONTRAGENT_ZIP','CONTRAGENT_CITY','CONTRAGENT_AREA','CONTRAGENT_STREET','CONTRAGENT_MOL','CONTRAGENT_PHONE','CONTRAGENT_BANK','CONTRAGENT_BANK_CODE','CONTRAGENT_BANK_ACCOUNT','CONTRAGENT_EXT_CODE','CONTRAGENT_EMAIL','PAYMENT_NAME','DOC_MARKER1','DOC_MARKER2','DOC_MARKER3','DOC_MARKER4','DOC_MARKER5','DOC_MARKER6','DOC_MARKER7','DOC_MARKER8','DOC_MARKER9','DOC_MARKER10');
insert into invoice values ('108826','ДАНЪЧНА ФАКТУРА','1','111366','2/15/2012','160.08','32.02','192.1','ПРЕЦИЗ ООД','103565237','BG103565237','ВАРНА ГЕН.КОЛЕВ 38','','','','','','','','','','','','БАНКА','','','','','','','','','','');
insert into invoice values ('');

[/text]

Re: Syntax error

Posted: Mon Mar 26, 2012 4:51 am
by social_experiment
krasi_e_d wrote:I have a problem with charset. When I start my code from browser the data in my table is ?????????, but when I paste the output file in phpadmin everything is ok.
This doesn't look like a syntax error though; Where does the data appear messed up: in the .sql file you create?

Re: Syntax error

Posted: Mon Mar 26, 2012 5:57 am
by krasi_e_d
social_experiment wrote:
krasi_e_d wrote:I have a problem with charset. When I start my code from browser the data in my table is ?????????, but when I paste the output file in phpadmin everything is ok.
This doesn't look like a syntax error though; Where does the data appear messed up: in the .sql file you create?
The data appear in the the same folder in output.sql. The data going in mysql database automaticaly, but in table invoice the text appear with ??????

Re: Syntax error

Posted: Mon Mar 26, 2012 6:00 am
by requinix
Is your CSV really in CP1251? Are you expecting Cyrillic?

Re: Syntax error

Posted: Mon Mar 26, 2012 6:03 am
by krasi_e_d
requinix wrote:Is your CSV really in CP1251? Are you expecting Cyrillic?
This is my csv file. I expecting cyrillic.
[text]
ID_PURCHASE_INVOICE,DOC_TYPE_NAME,DOC_EXT_CODE,DOC_NUM,DOC_DATE,WOVAT_AMOUNT,VAT_AMOUNT,TOTAL_AMOUNT,CONTRAGENT_NAME,CONTRAGENT_BULSTAT,CONTRAGENT_VATNUMBER,CONTRAGENT_POST_ADDRESS,CONTRAGENT_ZIP,CONTRAGENT_CITY,CONTRAGENT_AREA,CONTRAGENT_STREET,CONTRAGENT_MOL,CONTRAGENT_PHONE,CONTRAGENT_BANK,CONTRAGENT_BANK_CODE,CONTRAGENT_BANK_ACCOUNT,CONTRAGENT_EXT_CODE,CONTRAGENT_EMAIL,PAYMENT_NAME,DOC_MARKER1,DOC_MARKER2,DOC_MARKER3,DOC_MARKER4,DOC_MARKER5,DOC_MARKER6,DOC_MARKER7,DOC_MARKER8,DOC_MARKER9,DOC_MARKER10
108826,ДАНЪЧНА ФАКТУРА,1,111366,2/15/2012,160.08,32.02,192.1,ПРЕЦИЗ ООД,103565237,BG103565237,ВАРНА ГЕН.КОЛЕВ 38,,,,,,,,,,,,БАНКА,,,,,,,,,,

[/text]