pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: martin@axe.net.au
To: Gene Selkov Jr. <selkovjr@mcs.anl.gov>
To: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] placeholders
Date: Thu, 07 Jan 1999 10:52:34 +1100
Message-ID: <199901062355.KAA12338@axe.net.au> (raw)
In-Reply-To: <199901061604.KAA02423@antares.mcs.anl.gov>
References: <Your message of "Wed, 06 Jan 1999 18:01:35 +1100." <199901060704.SAA16837@axe.net.au>

At 03:05 7/01/99 , you wrote:
>> Hi,
>> 
>> I am using 
>> - Postrges 6.3.2
>> - DBI 0.93
>> - DBD-pg 0.73
>> - Slackware 3.3 Linux
>> 
>> I am accessing the database using a perl
>> CGI program.
>> 
>> I need to be able to list records from a 
>> database in various sort orders. The user
>> needs to be able to select the sort order
>> using a web form.
>
>[...]
>
>> 
>> I want to be able to change the code so the 
>> field name in the order by clause is variable
>> 
>> eg. instead of 
>> 	  ORDER BY title DESC
>> 
>> I want to say 
>> 	  ORDER BY :4 DESC   
>> 
>> and in the execute statement
>> $sth1->execute (("$category\%"),("$keywords\%"), ("$postedby\%"))
>> 
>> add a fourth variable ("$orderby") which 
>> will be set to title, dateposted or 
>> userlastupdate
>> 
>> However if I use ("$orderby") as the 
>> fourth variable and set $orderby to
>> title then my trace shows DBI puts 
>> 'title' into the SQL SELECT statement
>> not title and I get an invalid syntax
>> error. I need to find a way to stop
>> putting the ' ' around title.
>> 
>> Is there any way I can make the order by
>> operand a variable ??
>> 
>
>	What's wrong with:
>
>	@column = ("catalogid", "productid", ..., "category");
>
>	... ORDER BY $column[3] DESC ...

Gene, Thanks for your reply. 

I don't think I can put a perl variable $column[3]
into an sql prepare statement as it gets passed 
straight through as $column[3] to DBI and I get
a syntax error from DBI.
ERROR:  parser: parse error at or near "column"

However the sort in Perl looks like the 
answer so I will try that next.

Regards, Martin

>
>If you want it real smart, you can run a query to obtain column names
>from the database.
>
>Also, since you are using perl anyway, you might as well delegate
>ordering to perl. If you read your entire query output to an array of
>strings where values are delimited with something, e. g., "\t", you
>could say:
>
>	$colToSortOn = 2;
>	$descending = 1; # otherwise $descending = undef;
>	foreach ( sort {
>			@a = split "\t", $a; 
>			@b = split "\t", $b;
>		        ($descending ? $b[$colToSortOn] <=> $a[$colToSortOn] : $a[$colToSortOn] <=> $b[$colToSortOn])
>		} @result # @result comes from your query
>	) {
>		@values = split "\t";
>		# do your display stuff here
>	}
>
>See man perlfunc /sort SUBNAME for more details on sort()
> 



view thread (3+ messages)

Message-ID: <199901062355.KAA12338@axe.net.au>
Permalink:  ../199901062355.KAA12338@axe.net.au/
Also on:    postgresql.org/message-id/199901062355.KAA12338@axe.net.au

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: martin@axe.net.au, selkovjr@mcs.anl.gov
  Subject: Re: [SQL] placeholders
  In-Reply-To: <199901062355.KAA12338@axe.net.au>
  References: <199901062355.KAA12338@axe.net.au>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox