11.5. Retrieving Objects from the Database

When working with object-oriented PHP, it can be helpful to retrieve data from the table as objects. Therefore PHP provides a command called pg_fetch_object, which can be used like pg_fetch_array and pg_fetch_row. To see how the command works, first create a table and insert some data:

phpbook=# CREATE TABLE plant(id int4, name text, color text);
CREATE
phpbook=# INSERT INTO plant VALUES (1, 'Sambucus nigra', 'yellow');
INSERT 116394 1
phpbook=# INSERT INTO plant VALUES (2, 'Abies', 'green');
INSERT 116395 1
phpbook=# INSERT INTO plant VALUES (3, 'Colchicum autumnale', 'pink');
INSERT 116396 1

If no error occurred, you have created a table containing information about three plants and their colors. ...

Get PHP and PostgreSQL: Advanced Web Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.