0

I have several fields in a PostgreSQL db table which I want to access in a PHP loop, since I want the same things to happen to each of them.

They are named similarly (3 lowercase letters), eg "mor", "gls", etc, and the various names are stored in an array called $fields.

The core of the code I'm using is as follows (I'm retrieving each row from the table, and then trying to loop through the fields):

$sql="select * from mytable order by id";
$result=pg_query($db_handle,$sql);
while ($row=pg_fetch_object($result))
{
  foreach ($fields as $field)
  {
    $myfield=$row->$field;
    <do something with $myfield>
  }
}

Unfortunately, the $row->$field doesn't work. I get the following:

PHP Notice:  Undefined property: stdClass::$mor
PHP Notice:  Undefined property: stdClass::$gls

I've tried various permutations of brackets and quotes, but I can't seem to find the magic dust. Is this doable?

Thanks.

4 Answers 4

2

Why not fetch an array with pg_fetch_assoc instead of an object so you can just do;

$myfield=$row[$field];
Sign up to request clarification or add additional context in comments.

6 Comments

The point is that I want to work on the values of some fields without having to specify their names. If I specify the names, using $row->mor should be equivalent to your $row[mor]. But trying to use a variable there doesn't seem to work.
Simon's method should work, have you tried it? Somehow the object isn't liking your access to the variable property but I've never had a problem doing that with an array.
Yeah, and if you really, really don't want to request an array you could just cast the object to an array.
Yes, I had tried that, and it didn't work. But I've resolved it myself. I was reading the array from a file, and it appears that newlines were attached to the array items. Once I do: $myfield=trim($myfield); then $myfield=$row->$field; works as expected, and loops through the fieldnames. Thanks for your input!
Ah, that goes to show why you need to include all code... It's always the bit you don't show that was causing the problem.
|
1
var_dump( $row );

and check if $row is what you expect.

3 Comments

object(stdClass)#1 (11) { ... ["mor"]=> string(63) "blah blah" ... } Yes, it's coming back with the correct data.
Ok, but it has no field like mor - mor is a key of some array here - try $row[$field].
"mor", "gls", etc are 3-letter fields in the row, and are listed in the $fields array. I'm trying to use that to slot them into the $row-> construct so that I don't have to refer to each of them by name. Unfortunately $row[$field] doesn't work.
1

Try $myfield=$row->{$field}

2 Comments

What is the structure of mytable?
CREATE TABLE mytable ( id serial NOT NULL, filename character varying(50), surface text, mor text, gls text, <other three-letter names> comment text, ); I want to do similar things to all the fields with 3-letter names, which are stored in the array $fields. The main reason I want to loop through them is because this table will be built dynamically, and the number of 3-letter names may vary, and hitherto-unknown names may appear - the only constants are that they will all be 3 letters, and that they will be listed in $fields after earlier scanning of the file.
0

You should iterate through the row, it will give you the column names (key) and their values (value)

you cant iterate through $fields, as it does not exist.

$sql="select * from mytable order by id";
$result=pg_query($db_handle,$sql);
while ($row=pg_fetch_object($result))
{
  foreach ($row as $key => $value)
  {
    $myfield=$value;
    <do something with $myfield>
  }
}

1 Comment

I don't think so - I can do something to the value of $myfield, provided I specify it directly, if I use while ($row=pg_fetch_object($result)) { $myfield=$row->mor <do something> } But then I have to specify $row->gls directly, and the others. I'm trying to avoid doing that by having PHP loop through $row->x, where x is one of the items in the array $fields. It's this bit I'm having trouble getting to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.