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 8557909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:40:59+00:00 2026-06-11T15:40:59+00:00

I am returning a SETOF from a Postgres FUNCTION to PHP/PDO. Unfortunately, I am

  • 0

I am returning a SETOF from a Postgres FUNCTION to PHP/PDO. Unfortunately, I am getting 2 copies of every row returned.

Here is the Postgres TYPE:

CREATE TYPE marker AS (i BOOLEAN, r DOUBLE PRECISION, la DOUBLE PRECISION, lo DOUBLE PRECISION, n INTEGER);

And the FUNCTION:

CREATE OR REPLACE FUNCTION rl_select_markers (latitude DOUBLE PRECISION, longitude DOUBLE PRECISION) RETURNS setof marker AS $$
DECLARE
ROW marker%ROWTYPE;
BEGIN
    FOR ROW IN SELECT is_male, rate, lat, lon, (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - created_at)/60)::INTEGER FROM markers 
    WHERE expires_at > CURRENT_TIMESTAMP 
    ORDER BY ST_Distance(ST_Point(longitude, latitude), geog, FALSE) LIMIT 25 
    LOOP
    RETURN NEXT ROW;
    END LOOP;
    RETURN;
END;
$$ LANGUAGE plpgsql;

Here is the relevant PHP:

$dbh = new PDO('pgsql:host=' . $host . ';dbname=' . $db, $user, $pw);
$stmt = $dbh->prepare("SELECT rl_select_markers (:lat, :lon)");
$stmt->bindParam(':lat', $lat, PDO::PARAM_INT);
$stmt->bindParam(':lon', $lon, PDO::PARAM_INT);
$stmt->execute();

$keys = array('i', 'r', 'la', 'lo', 'n');
$markers = array();
while ($lines = $stmt->fetch()) 
{   
    $log->logInfo($log_id . " lines=" . $lines);
    $combined = array();
    foreach ($lines as $line) 
    { 
        $log->logInfo($log_id . " line=" . $line);
        // Remove brackets around $line and put in array
        $vals = explode(",", substr($line, 1, strlen($line)-2));
        // Combine into key value pairs
        $combined = array_combine($keys, $vals);
    }
    $markers[] = array("m"=>$combined);
}

$dbh = null;
$stmt = null;

$log->logInfo($log_id . " Send 200 OK");
sendResponse(200, json_encode(array("mks"=>$markers)), "application/json");

The unexpected behaviour is that each $lines is an array of two copies of the same line.

E.G. When there is one record to return $lines would be an array of 2 strings that are exactly the same. So the above logging will produce:

lines=Array
line=(f,10,51.505601,-0.109917,8)
line=(f,10,51.505601,-0.109917,8)

I understand my inner PHP loop is nonsense but it works. I can see work arounds (such as add a break;)but want to remove the duplication.

I hope I have explained this clearly. Can you see why I am getting these duplicates of every record?

Edit

Following @a_horse_with_no_name’s comment question:

Calling the SQL or the FUNCTION from phpPgAdmin does not produce the duplicate.

  • 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-06-11T15:41:01+00:00Added an answer on June 11, 2026 at 3:41 pm

    I think that you’ve made a mistake thinking that fetch() function would return multiple rows — it always returns one row — an array of columns.

    As default $fetch_style is PDO::FETCH_BOTH this array is indexed on both column name and 0-indexed column number and has two elements for every column:

    0 => (f,10,51.505601,-0.109917,8)
    'rl_select_markers' => (f,10,51.505601,-0.109917,8)
    

    So:

    • use fetch(PDO::FETCH_NUM), so you’ll get a number indexed column;
    • get rid of foreach ($lines as $line)
    • rename variable $lines to $line
    • just use $line[0]

    Corrected code:

    while ($line = $stmt->fetch(PDO::FETCH_NUM)) 
    {   
        $log->logInfo($log_id . " line=" . print_r($line,TRUE));
        $combined = array();
        // Remove brackets around $line and put in array
        $vals = explode(",", substr($line[0], 1, strlen($line[0])-2));
        // Combine into key value pairs
        $combined = array_combine($keys, $vals);
        $markers[] = array("m"=>$combined);
    }
    

    PHP GOTHA Nr 64372

    Even better: I’d use select (rl_select_markers (:lat, :lon)).* query and then just:

    while ( $combined = $stmt->fetch(PDO::FETCH_ASSOC) ) {
        $markers[] = array("m"=>$combined);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: returning multiple values from a function For example if you want a
I have a request.json in mootools to get some data from a php function
I'm trying to create a function that returns a dynamic setof data type. The
I am trying to select arrays in a set-returning function in postgres 8.4, but
I was wondering if there is a native PHP function for returning an array
I am returning a model from my controller that displays a grid. Works fine.
I have a service returning an array of type BaseItem. BaseItem has N subtypes.
NSString is returning as NULL while Importing from ViewController Class were i want to
I am loading a set of images from another page using jquery load() function.
What if I have few factory methods returning non-public type and pairing set of

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.