Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

selecting from a mysqldatabase

by bory (Beadle)
on Nov 24, 2003 at 09:38 UTC ( [id://309477]=perlquestion: print w/replies, xml ) Need Help??

bory has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have the following code:
my @compile=param ('compile'); my $dbh=DBI->connect($dsn,$user,$password,) or die "Error $DBI:: error + connecting to $dsn"; my $sth=$dbh->prepare("SELECT * FROM ddts WHERE `Stat`=\"T\" AND `Proj +ect`=\"Def\" AND `Version`=\"@version\""); $sth->execute(); print "<table border=2>"; while (my ($identifier)=$sth->fetchrow_array ) { foreach $elem(@compile){if ($elem eq $identifier){ print "<tr><td>$identifier</td></tr>";}}} print "</table>"; $sth->finish;
In @compile array I have what I selected with a checkbox from a form.I want to print from database only what I selected with the checkbox my code does this but it's printig each element twice.I don't know why> Thank you very much for your time.

Replies are listed 'Best First'.
Re: selecting from a mysqldatabase
by gmax (Abbot) on Nov 24, 2003 at 10:25 UTC

    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>";
     _  _ _  _  
    (_|| | |(_|><
     _|   
    
Re: selecting from a mysqldatabase
by Roger (Parson) on Nov 24, 2003 at 10:13 UTC
    I highly suspect that the error is with your foreach $elem(@compile) line. Stick use Data::Dumper to your code, and check the values of your @compile.

    use Data::Dumper; my @compile ... print "<pre>\n", Dumper(\@compile), "\n</pre>\n";
    Another possibility is that your table has duplicate data. To check for this, simply insert a print after the while loop -

    while (my($identifier)=...) { print "<tr><td><font color=#FF0000>", # <- insert print "$identifier</font></td></tr>\n"; foreach ... }
      I found the problem: i had had duplicate values in @compilea from my mistake.Now it's printing ok. Thank you very much for your help.
Re: selecting from a mysqldatabase
by Anonymous Monk on Nov 24, 2003 at 10:25 UTC
    1. @compile can contain anything, so you should really quote its value(s) when you use them in the prepare call (quote as in DBI->quote)
    2. $identifier can contain anything (you cannot rely on it returning column values in the order you expect). Be specific in your selects, don't select information you're never going to use (don't "select *" when all you don't want it)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://309477]
Approved by Roger
Front-paged by rnahi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found