Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: selecting from a mysqldatabase

by gmax (Abbot)
on Nov 24, 2003 at 10:25 UTC ( #309483=note: print w/replies, xml ) Need Help??


in reply to selecting from a mysqldatabase

Some things to consider:

  • If @compile has several items with the same value, then you have a duplicate.
  • if $identifier is duplicate in your table, then it is printed more than once.
  • You have @version in your query, but it does not seem to be declared. Moreover, @version is an array. Are you sure you want to use an array as a single value? Should it be $version instead?
  • "SELECT *" will get all the fields, without a guarantee of a particular order. Then your $identifier will get the first column in your result set, whichever it is. If you need only one column, name it in your SELECT.
  • Perl has several ways of quoting strings. Using qq{}, qq(), qq[],qq<> will let you use double quotes as literals inside your string.
  • Better yet, use $dbh->quote to quote your variables before merging them in a query string.
  • Even better yet, use placeholders. See below for an example and DBI Recipes for a wider explanation.
  • The error string for DBI comes with $DBI::errstr, not "DBI:: error"

Here is an (untested) rewrite of your code.

Make sure you understand what is there. See our Tutorials page for more basics on database programming with Perl.

my @compile=param ('compile'); my $version = "whatever"; my $dbh=DBI->connect($dsn,$user,$password,) or die "Error $DBI::errstr connecting to $dsn"; my $sth=$dbh->prepare(qq{ SELECT mycolumn FROM ddts WHERE `Stat`= ? AND `Project`= ? AND `Version`= ? }); $sth->execute("T", "Def", $version); print "<table border=2>"; while (my ($identifier) = $sth->fetchrow_array ) { foreach my $elem (@compile) { if ($elem eq $identifier) { print "<tr><td>$identifier</td></tr>"; } } } print "</table>";
 _  _ _  _  
(_|| | |(_|><
 _|   

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://309483]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2022-12-10 04:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?