dougudu3564 2013-10-20 18:46
浏览 29

PostgreSQL数据库中的第一条记录不被PHP代码读取

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?

  • 写回答

2条回答 默认 最新

  • douzhaiyuan1731 2013-10-20 18:48
    关注

    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
    
    评论

报告相同问题?