I have a PHP script which inserts a row into a Postgres DB. It works fine on my test server (Postgres 9.1) but fails on my new shared host (Postgres 8.4).
$query = "INSERT INTO " . $table . " VALUES (DEFAULT, $1, $2, CURRENT_TIMESTAMP::TIMESTAMP(0), $3, $4, ST_Point($4, $3))";
$result = pg_send_query_params($dbconn, $query, array($is_happy, $rate, $lat, $lon));
The $result
is always TRUE but no row is inserted.
I also tried:
$result = pg_get_result($dbconn);
But experienced the same behaviour.
Following @CraigRinger's transaction suggestion below I also tried both:
$query = "BEGIN;INSERT INTO " . $table . " VALUES (DEFAULT, $1, $2, CURRENT_TIMESTAMP::TIMESTAMP(0), $3, $4, ST_Point($4, $3));COMMIT;";
$result = pg_send_query_params($dbconn, $query, array($is_happy, $rate, $lat, $lon));
and
pg_send_query($dbconn, "BEGIN;");
$query = "INSERT INTO " . $table . " VALUES (DEFAULT, $1, $2, CURRENT_TIMESTAMP::TIMESTAMP(0), $3, $4, ST_Point($4, $3));";
$result = pg_send_query_params($dbconn, $query, array($is_happy, $rate, $lat, $lon));
pg_send_query($dbconn, "COMMIT;");
And still got the same behaviour.
If I run the INSERT query in phpPgAdmin a row is inserted. I am also able to get the results from this query in a PHP script:
"SELECT relid FROM pg_stat_user_tables"
What does this mean when the INSERT query result is true but no row is inserted? What could be the problem and how might I approach resolving this?
Edit
I can INSERT
rows using phpPgAdmin and then SELECT
these rows from a PHP script.
Here is a full PHP script that fails (it is stripped of POST gathering and validation that exists in my full script). The rest.php
script simply holds connection vars and a response function. This rest.php
script works with my SELECT
script:
<?php
require "KLogger.php";
require "rest.php";
ini_set("error_reporting", E_ALL ^ E_NOTICE);
ini_set("display_errors", 0);
ini_set("log_errors", 1);
$log = KLogger::instance(dirname(__FILE__), KLogger::DEBUG);
$log_id = "AM".time();
$log->logInfo($log_id . " *** " . $_SERVER['REMOTE_ADDR']);
$lat = '51.510199';
$lon = '-0.129654';
$rate = '10';
$is_happy = 't';
$dbconn = pg_connect("host=" . $host . " dbname=" . $db . " user=" . $user . " password=" . $pw);
if(!$dbconn)
{
$log->logInfo($log_id . " No connection: " . pg_last_error());
sendResponse(500, "Internal Server Error");
}
else
{
$query = "INSERT INTO " . $table . " VALUES (DEFAULT, $1, $2, CURRENT_TIMESTAMP::TIMESTAMP(0), $3, $4, ST_Point($4, $3));";
$result = pg_send_query_params($dbconn, $query, array($is_happy, $rate, $lat, $lon));
if(!$result)
{
$log->logInfo($log_id . " No Result: " . pg_last_error());
sendResponse(500, "Internal Server Error");
}
else
{
$log->logInfo($log_id . " sendResponse(200)");
sendResponse(200, "OK");
}
}
pg_close($dbconn);
?>