Use php to display table list from PostgreSQL

Posted: September 3, 2010 in Uncategorized

Often, I find myself re-using code over and over.  I thought I would share some really really … poor code with you.  However, it gives you an example of ONE way that does work.

<html>
<title>Table List</title>
<?php

$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'itemlist.php';

$qs = $_SERVER["QUERY_STRING"];
$qs_array = explode("&",$qs);

$db = "inventory";

if (isset($_GET["p"])) { $p = $_GET["p"] ;} else { $p = "1";}
if (isset($_GET["id"])) $id = $_GET["id"];
if (isset($_GET["a"])) { $action = $_GET["a"];} else { $action = "n";}
if (isset($_GET["table"])) {$tbl = $_GET["table"];} else { $tbl = "character";}
if (isset($_GET["idcol"])) { $idcol = $_GET["idcol"];} else { $idcol = "char_id";}

$pre_where = "";
$post_where = "";

if (isset($id)) {
$pre_where = " WHERE " . $idcol . " = " . $id;
$post_where = " WHERE " . $idcol . " = " . $id;
} else {
$pre_where = " WHERE 0 = 1";
$post_where = " WHERE 0 = 1";
}

setlocale(LC_MONETARY, 'en_US');
// Connecting, selecting database
$dbconn = pg_connect("host=localhost dbname=$db user=username password=password")
or die('Could not connect: ' . pg_last_error());

$query = "select tablename from pg_tables where tableowner='username' order by tablename";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
?>
<a href="index.php">back</a>
<table border="1">
<?
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
?>
<tr>
<td><? echo $line['tablename']; ?></td>
<td><a href="list.php?table=<? echo trim($line['tablename']); ?>">list</a></td>
</tr>

<?
}
?>
</table>
</html>

*** updated *** this one uses PostgreSQL not mySQL.

Advertisement
Comments
  1. [...] This post was mentioned on Twitter by Jonathan Day, Jonathan Day. Jonathan Day said: @Eric_Allison http://bit.ly/9MNVj0 Here's and example of my php style. Notice the in-line php. [...]

  2. weight says:

    yeah my dad will like this

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s