begin.py
begin.py

Reputation: 161

Obtain Column name(s) of database via PHP

I have a postgresql database and I am connecting to and reading from it via php. Ive put in php codes that give me back the result to the query i pass from my code.

Ex- My code :(Note - My HTML page uses a form which asks for input and searches for the given input in the database)

<?php
        $result = pg_prepare($dbh, "Query1", 'SELECT * FROM test.bact WHERE disease = $1');

        $result = pg_execute($dbh, "Query1", array($disease));
        if (!$result) {
                die("Error in SQL query: " . pg_last_error());
        }
        //$rows = pg_fetch_all($result)

/*// iterate over result set
// print each row*/
            while ($row = pg_fetch_array($result)) {
                    echo $row[0]." ".$row[1]. "<br />";
                } 

From the above piece of code I get my information as strings separated by a space ( echo $row[0]." ".$row[1])

example: Information at row[0]<space>Information at row[1]

What I want - I want the retrieved data in a more organised form i.e. with the column name. How it should look like -

Name of Column : Data 
Name of column : Data ...and so on.

I know there is way in mysql using the mysql_fetch_field, but I wanted something for postgresql. Since I am new to php n databases I am not really sure as to how will I use this.

Any help would be appreciated.

Upvotes: 2

Views: 1455

Answers (1)

Jeka
Jeka

Reputation: 1374

You can use pg_field_name or pg_fetch_assoc

Upvotes: 4

Related Questions