Re: MySQL --> PostgreSQL with PHP

From: Mark Kelly <pgsql(at)wastedtimes(dot)net>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: MySQL --> PostgreSQL with PHP
Date: 2010-10-08 22:47:06
Message-ID: 201010082347.06627.pgsql@wastedtimes.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi.

On Friday 08 Oct 2010 at 22:42 Helgi Örn Helgason wrote:

> Hi!
> Anyone who can spot what I'm doing wrong here?

[big snip]

Try this (easier for me to show than explain):

//============ START CODE ===================
// Connect to the PostgreSQL server
// Build a string to specify the connection parameters, NOT pass them as args.
$connectionString = "host=$hostName dbname=$databaseName ".
"user=$username password=$password";
if (!$connection = pg_connect($connectionString)) {
die("Cannot connect: ".pg_last_error());
}

// Run the query on the connection
$query = "SELECT * FROM timmar";
// You had $connection and $query in the wrong order here.
if (!$result = pg_query ($connection, $query)) {
die("Query failed: ".pg_last_error());
}

// Display the results
displayTimmar($result);
//============ END CODE ===================

I added some error feedback that I suspect may come in useful while you are
working on the code, removed some @ and () you didn't need, and added some {}.

For full details of the pg_* commands start here:
http://uk.php.net/manual/en/ref.pgsql.php

For the functions you are using, here:
http://uk.php.net/manual/en/function.pg-connect.php
and here:
http://uk.php.net/manual/en/function.pg-query.php

If the table is small (I assume it is since you are sticking the whole thing
in a HTML table) you might also want to consider fetching all the results at
once with pg_fetch_all then using a simple foreach in displayTimmar() rather
than hitting the database for each row individually.

Cheers,

Mark

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message richard terry 2010-10-09 02:45:36 Like and Not LIke
Previous Message Helgi Örn Helgason 2010-10-08 21:42:11 MySQL --> PostgreSQL with PHP