OrhunB
OrhunB

Reputation: 11

Warning: pg_query() [function.pg-query]: Query failed: ERROR: unterminated quoted string at or near

I have a query where I get the error:

The code is:

$prefix = $data->sheets[0]['cells'][$row][2];
$fiyat = $data->sheets[0]['cells'][$row][3];
$increment = $data->sheets[0]['cells'][$row][4];
$tarih = $data->sheets[0]['cells'][$row][5];
$tarih = explode('.',$tarih);
$effective_date = $tarih[2] . "-" . $tarih[1] . "-" . $tarih[0];

All variables returns correct values (and variable types).

$query_insert_to_rates = "INSERT INTO rates VALUES (nextval('routes_seq'), '$prefix', '$i_tariff', '$fiyat_orj', '$fiyat_orj', '$increment', '$increment', '$forbidden', 't', '0', '0', '$increment', '$increment', NULL, '$eff_date_ins', NULL);";

$result_insert_to_rates = pg_query($query_insert_to_rates);

INSERT INTO rates VALUES (nextval('routes_seq'), '21321', '8', '0.0470', '0.0470', '1', '1', 't', 't', '0', '0', '1', '1', NULL, '2012-06-01 00:00:00.000001+02', NULL);

Warning: pg_query() [function.pg-query]: Query failed: ERROR: unterminated quoted string at or near "'2" at character 50 in.....

Now, when I run the query within the PHP, I get the above error. My PHP Version is 5.3.10. And the machine is Centos 5 (VPS).

However if I run the query through Navicat(connected to the DB) then I have no error, and query executed perfectly.

Any help would be appreciated.

Upvotes: 1

Views: 4345

Answers (1)

Pavel Stehule
Pavel Stehule

Reputation: 45805

This is terrible code: unsafe and insecure. Don't use this pattern ever

$SQL = "INSERT ... ('$var1', '$var2')

use PDO parametrized queries or pg_escape_string function instead

pg_escape_string

Upvotes: 2

Related Questions