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

Member Avatar for diafol

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());

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.