Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3232066
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:07:05+00:00 2026-05-17T17:07:05+00:00

I have the following table in PostgreSQL 8.4.5: snake=> create table gps ( id

  • 0

I have the following table in PostgreSQL 8.4.5:

snake=> create table gps (
id bytea check(length(id) = 16),
stamp timestamp DEFAULT current_timestamp,
pos point not null);

and I’m able to INSERT record into it from psql prompt:

snake=> insert into gps (id, pos) values (decode(md5('x'), 'hex'), point(0, 0));
INSERT 0 1
snake=> insert into gps (id, pos) values (decode(md5('x'), 'hex'), point(0, 0));
INSERT 0 1

But for some reason INSERT fails in my PHP script listed below and its result is returned as 0. Does anybody please have an idea what is wrong there or how to get more info? I’m surprised that an exception isn’t thrown.

<?php

$id  = trim($_REQUEST['id']);
$lat = strtr(trim($_REQUEST['lat']), ',', '.');
$lon = strtr(trim($_REQUEST['lon']), ',', '.');

if (preg_match('/^[a-fA-F0-9]{32}$/', $id) &&
    preg_match('/^[+-]?[0-9.]+$/', $lat) &&
    preg_match('/^[+-]?[0-9.]+$/', $lon)) {

        try {
                $db = new PDO('pgsql:host=/tmp', 'snake', 'snake');
                $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $insert = $db->prepare("insert into gps (id, pos) values (decode(?, 'hex'), point(?, ?))");
                $res = $insert->execute($id, $lat, $lon);

                $select = $db->prepare("select encode(id, 'hex') as id, extract('epoch' from stamp) as stamp, pos[0] as lat, pos[1] as lon from gps");
                $select->execute();

                header('Content-Type: text/xml; charset=utf-8');
                print '<?xml version="1.0"?><gps>';
                while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
                        printf('<pos id="%s" stamp="%u" lat="%f" lon="%f" />',
                            $row['id'], $row['stamp'], $row['lat'], $row['lon']);
                }
                printf('<res val="%d" />', $res);

                print '</gps>';
        } catch (Exception $e) {
                print 'Database problem: ' . $e->getMessage();
        }

} else {
        header('Content-Type: text/html; charset=utf-8');
        print '<html>
<body>
<form method="post">
<p>Id: <input type="text" name="id" size=32 maxlength=32 /></p>
<p>Latitude: <input type="text" name="lat" /></p>
<p>Longitude: <input type="text" name="lon" /></p>
<p><input type="submit" value="Save" /></p>
</form>
</body>
</html>
';

}

?>

I get the output which indicates that result is 0:

<gps>
<pos id="0cc175b9c0f1b6a831c399e269772661" stamp="1287306960" lat="51.000000" lon="7.000000"/>
<pos id="0cc175b9c0f1b6a831c399e269772661" stamp="1287323377" lat="51.000000" lon="7.000000"/>
<pos id="92eb5ffee6ae2fec3ad71c777531578f" stamp="1287323381" lat="51.000000" lon="7.000000"/>
<pos id="92eb5ffee6ae2fec3ad71c777531578f" stamp="1287323442" lat="51.300000" lon="7.000000"/>
<pos id="92eb5ffee6ae2fec3ad71c777531578f" stamp="1287325610" lat="51.300000" lon="7.000000"/>
<pos id="92eb5ffee6ae2fec3ad71c777531578f" stamp="1287325612" lat="51.300000" lon="7.000000"/>
<pos id="9dd4e461268c8034f5c8564e155c67a6" stamp="1287325692" lat="0.000000" lon="0.000000"/>
<res val="0"/>
</gps>

Regards,
Alex

PS: Here is my current script, seems to work ok –

<?php

$id  = trim($_REQUEST['id']);
$lat = strtr(trim($_REQUEST['lat']), ',', '.');
$lng = strtr(trim($_REQUEST['lng']), ',', '.');

if (preg_match('/^[a-fA-F0-9]{32}$/', $id) &&
    preg_match('/^[+-]?[0-9.]+$/', $lat) &&
    preg_match('/^[+-]?[0-9.]+$/', $lng)) {

        try {
                # enable persistent connections and throw exception on errors
                $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                                 PDO::ATTR_PERSISTENT => true);

                $db = new PDO('pgsql:host=/tmp dbname=snake', 'snake', 'snake', $options);

                #$db->exec('create table gps (id bytea check(length(id) = 16), stamp timestamp DEFAULT current_timestamp, pos point not null)');

                $insert = $db->prepare("insert into gps (id, pos) values (decode(?, 'hex'), point(?, ?))");
                $insert->execute(array($id, $lat, $lng));

                $select = $db->prepare("select encode(id, 'hex') as id, extract('epoch' from stamp) as stamp, pos[0] as lat, pos[1] as lng from gps");
                $select->execute();

                header('Content-Type: text/xml; charset=utf-8');
                print '<?xml version="1.0"?><gps>';
                while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
                        printf('<pos id="%s" stamp="%u" lat="%f" lng="%f" />',
                            $row['id'], $row['stamp'], $row['lat'], $row['lng']);
                }
                print '</gps>';
        } catch (Exception $e) {
                print 'Database problem: ' . $e->getMessage();
        }

} else {
        header('Content-Type: text/html; charset=utf-8');
        print '<html>
<body>
<form method="post">
<p>Id: <input type="text" name="id" size=32 maxlength=32 /></p>
<p>Latitude: <input type="text" name="lat" /></p>
<p>Longitude: <input type="text" name="lng" /></p>
<p><input type="submit" value="Save" /></p>
</form>
</body>
</html>
';
}

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:07:05+00:00Added an answer on May 17, 2026 at 5:07 pm

    I believe you need to do PDOStatement->insert() using an array of parameters, not multiple arguments:

    $res = $insert->execute($id, array($lat, $lon));
    

    See the manual.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PostgreSQL table with the following schema - CREATE TABLE test (
I have a table in postgresql. The following table animals will do to explain
I have the following table of counters: CREATE TABLE cache ( key text PRIMARY
Given the following table in PostgreSQL, how do I insert a record which refers
I have the following database table with information about people, diseases, and drugs: PERSON_T
Let's say I have the following objects: squirrel_table - name - country_of_origin - id
Is there a way to add a not null type column to a Postgresql
I'm using ActiveRecord / NHibernate for ORM mapping in my project with PostgreSQL 8.4.
I'm trying to optimize SQL queries in Akonadi and came across the following problem
I don't know much about database optimization, but I'm trying to understand this case.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.