I have a PostgreSQL database with some information about projects contained with in it. So each project has a name, description and contact but I'm looking to just pull the name attribute from the table using PHP.
I currently have two projects in my database.
ID | Name |
1 | Fruit project |
2 | Vegetable project |
And I have a PHP script below which generates
$res = pg_query("SELECT * FROM projects");
$assoc = pg_fetch_assoc($res);
$result = $assoc['name'];
/* FETCHES THE RESULT OF THE SQL QUERY WHICH GETS THE NAME OF EACH PROJECT */
while($row = pg_fetch_assoc($res))
{
$output[]=$row['name'];
print (json_encode($output));
} /* CONVERTED ON MOBILE PLATFORM */
But the output of that file is current 'Vegatable project'. Could any provide some help on why the script isn't producing the first result as well?
Because you're stripping it out off the result before your loop:
$res = pg_query("SELECT * FROM projects");
$assoc = pg_fetch_assoc($res); // <-- remove
$result = $assoc['name']; // <-- remove
You call pg_fetch_assoc
one time before the clycle - thet is the point where your first record is lost. Just remove $assoc = pg_fetch_assoc($res);