0

Hi All

First post. PHP/PostgreSQL newbie...

I'm trying to create a simple web page with a dropdown menu. The values for the dropdown menu are retrieved from a database table. The code I've got is...

<html>
        <body>
                <select>
                        <?php
                                $db = pg_connect("host=hostname port=5432 dbname=dbname user=user password=password") or die ("Could not connect to DB");
                                $sql = pg_query(sprintf("SELECT hostname FROM pkgchk"));
                                while ($row = pg_fetch_assoc($sql))
                                {
                                        echo "<option value=$row[hostname]>$row[hostname]</option>";
                                }
                                pg_close($db);
                        ?>
                </select>
        </body>
</html>

I've racked my brain and done a lot of googling, and I can't understand what's wrong.

I suspect the DB connection is not working. However, if I execute the file from the command line, I get...

[root@svr-app-nagios html]# php -f file.html
<html>
        <body>
                <select>
                        <option value=test>test</option>                </select>
        </body>
</html>

...which looks OK (there's only 1 record so far in the DB, test), so I presume the DB connection is OK?

Many thanks in advance

Stephen

Edited by steevod

2
Contributors
2
Replies
16
Views
3 Years
Discussion Span
Last Post by steevod
0

Can you add or die() statements to the connection and the query?

Example from the manual:

$dbconn = pg_connect("host=localhost dbname=publishing user=www password=foo")
    or die('Could not connect: ' . pg_last_error());

// Performing SQL query
$query = 'SELECT * FROM authors';
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
0

Thanks

I've made the changes requested above...

$db = pg_connect("host=hostname port=5432 dbname=dbname user=user password=password") or die ("Could not connect to DB" . pg_last_error());

...and...

 $sql = pg_query(sprintf("SELECT hostname FROM pkgchk")) or die('Query failed: ' . pg_last_error());
            while ($row = pg_fetch_assoc($sql))

...but no joy unfortunatly. Everything seems to run fine when I execute from the command line, but my dropdown menu is still not working.

Thanks

Stephen

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.