<head>
<title>Managing a PostgreSQL database table</title>
</head>
<?php 
// Simple testscript for managing a PostgreSQL database table.
// v0.21 13-Aug-2002, by G. Knauf <efash@gmx.net>
$host='localhost';
$user='postgres';
$pass='';
$dbase='webauth';
$table='user_pwd';
// Connecting to PostgreSQL database
$conn = pg_connect("host=$host dbname=$dbase user=$user password=$pass ") or die("Could not connect to PostgreSQL server $host!\n");

if (isset($_POST['submit'])) { 
    $result = pg_query("INSERT INTO $table (name,pass) ".
            "VALUES ('".$_POST['username']."','".$_POST['userpass']."')");
}

$result = pg_query("SELECT * FROM $table");
if (!$result) {
    echo "Error occured.\n";
    exit;
}

$num = pg_num_rows($result); 
$nfields = pg_num_fields($result); 

print "<h3>Database <font color=red>$dbase</font></h3>\n";
print "<h3>Table <font color=green>$table</font> contains <font color=blue>$num</font> records with <font color=blue>$nfields</font> fields:</h3>\n";
print "<table border>\n<tr>";

for ($i=0; $i < $nfields; $i++) {
  echo '<th>'.pg_field_name($result, $i).'</th>';
}
print "</tr>\n";

for ($i=0; $i < $num; $i++) {
  echo '<tr>';
  $r = pg_fetch_row($result, $i);

  for ($j=0; $j < count($r); $j++) {
    print "<td>$r[$j]&nbsp;</td>";
  }

  print "</tr>\n";
}
print "</table>\n";
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<b>Add a new user:</b><br>
<input type="text" name="username" size="30"><br>
<b>Enter password for new user:</b><br>
<input type="password" name="userpass" size="30">
<p><input type="submit" name="submit" value="submit">
</form>
Published in: MySQL
Download

Related snippets