Re: MySQL --> PostgreSQL with PHP - Mailing list pgsql-novice

From Mark Kelly
Subject Re: MySQL --> PostgreSQL with PHP
Date
Msg-id 201010082347.06627.pgsql@wastedtimes.net
Whole thread Raw
In response to MySQL --> PostgreSQL with PHP  (Helgi Örn Helgason <sacredeagle@gmail.com>)
Responses Re: MySQL --> PostgreSQL with PHP  (Helgi Örn Helgason <sacredeagle@gmail.com>)
Re: MySQL --> PostgreSQL with PHP  (Helgi Örn Helgason <sacredeagle@gmail.com>)
List 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

pgsql-novice by date:

Previous
From: Helgi Örn Helgason
Date:
Subject: MySQL --> PostgreSQL with PHP
Next
From: richard terry
Date:
Subject: Like and Not LIke