我想逐行读取我的文本文件然后保存到postgresql数据库。但是我得到了这些错误。
"Warning: pg_query() [function.pg-query]: Query failed: ERROR: syntax error at or near "," LINE 2: VALUES (1113, , 21, , ); ^ in C:\xampp\htdocs\phpsave\index.php on line 28
ERROR: syntax error at or near "," LINE 2: VALUES (1113, , 21, , ); ^1114rejie23Dimakita300000
Warning: pg_query() [function.pg-query]: Query failed: ERROR: syntax error at or near "," LINE 2: VALUES (1114, , 23, , ); ^ in C:\xampp\htdocs\phpsave\index.php on line 28
ERROR: syntax error at or near "," LINE 2: VALUES (1114, , 23, , ); ^1115James24Dimakita2300000
Warning: pg_query() [function.pg-query]: Query failed: ERROR: syntax error at or near "," LINE 2: VALUES (1115, , 24, , ); ^ in C:\xampp\htdocs\phpsave\index.php on line 28
ERROR: syntax error at or near "," LINE 2: VALUES (1115, , 24, , ); ^"
我的代码有什么问题?这是我的示例文本文件:
<?php
$host = "host=localhost";
$port = "port=5432";
$dbname = "dbname=sample";
$credentials = "user=postgres password=12345";
$fh = fopen('C:/Users/DecryptDcode/Desktop/james.txt','r');
$db = pg_connect("$host $port $dbname $credentials");
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
while (!feof($fh) ) {
$line_of_text = fgets($fh);
$parts = explode('|', $line_of_text);
print $parts[0] . $parts[1]. $parts[2] . $parts[3]. $parts[4]. "<BR>";
$sql =<<<EOF
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES ($parts[0], $part[1], $parts[2], $part[3], $part[4]);
EOF;
$ret = pg_query($db, $sql);
if(!$ret){
echo pg_last_error($db);
} else {
echo "Records created successfully\n";
}
}
pg_close($db);
fclose($fh);
?>
答案 0 :(得分:0)
很明显,您的代码不会在位置1,3和4中插入任何数据。错误消息只显示逗号,两者之间没有任何内容。代码中的违规语句是:
onclick
在第1,3和4部分添加一些ses和一些单引号,你很好:
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES ($parts[0], $part[1], $parts[2], $part[3], $part[4]);
让你想知道为什么PHP不会自己提起这个......