Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I use php to connect to a postgresql table 1

Status
Not open for further replies.

gglgokop1704

Programmer
Aug 9, 2007
54
GB
Dear All,

Please how can I use PHP to connect to a postgresql database/table and insert/delete data or retrieve data.Is it better to use ASP? Can ASP work in Linux environment?

Any help is appreciated.
Gokop
 
<?php

$dbconn = pg_connect("host=IP ADDRESS dbname=DBNAME user=USERNAME password=PASSWORD")
or die('Could not connect: ' . pg_last_error());

$query = "SELECT field1 from table";

$result = pg_query($query) or die('Query failed: ' . pg_last_error());
$num=pg_num_rows($result);
$i = 0;
while ($i <$num) {
while ($line = pg_fetch_array($result)) {
$field1 = $line['field1'];
$i = $i + 1;
}
}

?>

prettymuch

 
Dear All,

Thanks MercJones, I have used the code you sent to customise it according to my details but Please how can I run the PHP program to connect to a PostgreSQL database and select contents of a table and print it on the browser. I could not run the program. I don't know how to run it or perhaps how to activate the php. I have done some configurations on httpd.conf and php.ini files after installing php. See the program below:

<?php
// Connecting, selecting database
$dbconn = pg_connect("host=138.250.104.227 dbname=rftDatabase user=globus password=globususer");


// Performing SQL query
$query = 'SELECT * FROM Criteria_Parameters';
$result = pg_query($query);

// Printing results in HTML
echo "<table>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";

// Free resultset
pg_free_result($result);

// Closing connection
pg_close($dbconn);
?>

Is it possible to put the code int a function and then execute the function on a click of a button?

Any help is appreciated
Regards
Gokop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top