Forum Moderators: coopster

Message Too Old, No Replies

Postgresql, Extracting information and making a link work

Postgresql, Extracting information and making a link work

         

Imy_S3

1:12 pm on Feb 3, 2004 (gmt 0)

10+ Year Member



Hi

I have a postgresql database.

I have a search facility on there that retrives information from this database.

One peice of information that is retrived is a web site address.

So I would have:

JJB sports, London, www.jjbsports.co.uk

How do i get it so the user is able to click on the www.jjbsports.co.uk and then visit the web site.

Thanks in advance.

coopster

1:14 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Imy_S3!

You need to:

  1. Connect to your database (pg_connect [php.net])
  2. Build a query statement
  3. Execute the statement (pg_query [php.net])
  4. Process the returned Result Set (pg_fetch_array [php.net] OR pg_fetch_all [php.net])

There is a tutorial in the PHP Library for Basics of extracting data from MySQL using PHP [webmasterworld.com] that will help you plenty. The practice detailed there is much the same for Postgresql. Substitute the mysql functions with the PostgreSQL functions [php.net].

That reading should get you started. Now here's some direct application including a little code snippet that can follow. Let's say the table that you are storing your data in is called

companies
and the column names in the table are
company
,
city
, and
url
. I'm not sure which parameters you need to make a connection to your database so you may have to make that adjustment:

<?php
$company = 'JJB Sports';
$host = 'localhost';
$port = '5432';
$dbname = 'mydatabasename';
$user = 'myuserid';
$password = 'mypassword';
$dbconn = pg_connect [php.net]("$host $port $dbname $user $password");
if (!$dbconn) exit("A connection error occurred.");
$sql = "SELECT city, url FROM companies WHERE company = '$company'";
$rows = pg_query [php.net]($dbconn, $sql);
if (!$rows) exit("A query execution error occurred.");
$row = pg_fetch_array [php.net]($rows, 1, PGSQL_ASSOC);
print "You can visit $company in " . htmlentities [php.net]($row['city']);
print "<br />You can visit $company online at " . htmlentities [php.net]($row['url']);
pg_close($dbconn);
?>