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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: mysql database
by rnahi (Curate) on Dec 12, 2003 at 14:42 UTC

    You are making the same mistakes that you made in a previous node (selecting from a mysqldatabase).

    For starting, your SQL is not valid. Moreover, you are making a Cartesian product with your query. It means that if your tables ddts and deliver have 1000 records each, the query will return 1 million records.

    There are other problems as well. It seems to me that you are doing a lot of cargo cult programming. Read again the answers to your previous post and then ask again what you need.

      I don't think your numbers are quite right on the query. He is checking AND delivery.Data=ddts.Data which will limit the data to just records that are 'linked' between the tables by there data field. Of course i don't know how valid that link is, but unless every record in both tables has the same data feild he should be okay. (Ignoring of course the comparison of `User`=\"@user\", i have no idea what thats suppose to do.


      ___________
      Eric Hodges
Re: mysql database
by jdtoronto (Prior) on Dec 12, 2003 at 15:17 UTC
    bory,

    You are making a number of crucial mistakes. Firstly you are directly placing variables into your prepare string. Use placeholders instead then the DBI/DBD will take care of escaping for you. You haven't fully worked out the SQL you need and until you do it is likely you will have some pretty awful problems!

    First step is to check the tutorials on this site. You are making errors and using a coding style which is well discussed in several tutorials. Either go to www.mysql.com and carefully read the documentation there or go out and buy a good MySQL or SQL book.

    You also have a similar quoting problem in building the checkboxes. I would suggest using CGI.pm to create them, which will, again, removing the quoting difficulties.

    And remember - ONE WORD for me please - PLACEHOLDERS

    jdtoronto

Re: mysql database
by Abigail-II (Bishop) on Dec 12, 2003 at 14:37 UTC
    Showing the code is one step, but all you say about it is that you have a problem. But you don't say what it is. Does it crash? Does it print the wrong thing? (If so, what does it print?) Do you get errors? Does it return the wrong things from the database?

    Also, you aren't checking whether your prepare succeeds; you aren't checking whether your execute succeeds, and you aren't using placeholders.

    Abigail

Re: mysql database
by Sandy (Curate) on Dec 12, 2003 at 16:40 UTC
    Ok, I'm going to assume that you have fixed the SQL statements, and the checkbox creations prints, as per instructed in the previous nodes

    About the actual algorithm...

    If i understand your problem correctly, you want to
    print a list of all of the items in @id
    and
    if this $id[$i] is also in the database (identified by variable $identifier)
    print $id with a checked checkbox,
    else print $id without an unchecked checkbox.

    If this is what you want to do, then...
    1. Loop through @id,
    1.1 Query database using your $id as a search criteria,
    1.2 If there is a match, then
    1.2.1 print a checked box with $id
    1.3 If there is no match, then
    1.3.1 print an unchecked box.

    It this is not what you want to do, just forget I mentioned it.

    PS: If @id is huge, then another algorithm may be more appropriate, which minimizes all the queries to the database.

    My $.02 worth: When I was first learning to program, my mentor insisted that I write my algorithm 1st, and then step through it with 'dummy data' while pretending to be the computer (do what I say, not what I mean). This method works well when you are still at the learning phase of programming.

      I found how to do it but it's printig the values that are in the database twice:once checked and once unchecked.I don't know how to print them only once as checked
      for (my $i=0; $i<=$#id; $i++){ my $sth=$dbh->prepare("SELECT Data,Identifier,User,Version FROM ddts,d +elivery WHERE `User`=\"@user\" AND `Identifier`=\"$id[$i]\" AND deliv +ery.Data=ddts.Data"); $sth->execute(); while (my ($data,$identifier,$user,$version)=$sth- +>fetchrow_array ) { print "<tr><td><input type=\"checkbox\" name= +\"rt\" CHECKED value=\"$identifier\"> $identifier</td></tr>";} if ($id[$i] ne $identifier){ print "<tr><td><input type=\"checkbox\" name=\"rt\ +" value=\"$id[$i]\"> $id[$i]</td></tr>"; } + $sth->finish; }
      Thank you very much
        Maybe(?) your query match to $identifier has extra spaces?

        I will be willing to help you solve this if you want (later this week, not now), but I would need to know a bit more info.

        Could you give me a sample of the input from the db (not the whole thing please), explain why you are querying based on 'user'=\"@user\", print out the select statement before querying, and print the results (as is) of each row fetched from the database.

        It's possible that just doing this may help you solve the problem yourself.

        UPDATE ... ignore above

        Silly me, over lunch I realized what was wrong (at least I think that this is the problem).

        When you query the database, you use a while loop. When a row is returned, you print the resulting CHECKED box. So far so good.

        But, still within the while loop, a new query is performed, and there is no row returned. Now the $identifier is 'undef', and the while loop is exited.

        So what happens next? The if statement is executed, and $identifier (which is 'undef' because of the last query to the db) is not equal to $id. The condition is true, and the unchecked box is printed.

        There are certainly a lot better ways of doing this than what I am about to suggest, (it's a quick and dirty fix) but it should work.

        PS: Note that I used a placeholder, which many people have mentioned in the other posts.
        PSS: I don't have a db to play with, so I think that this works, but I actually haven't checked the syntax of the execute statement. Please check before using!

        my $sth=$dbh->prepare( "SELECT Data,Identifier,User,Version FROM ddts, ". "delivery WHERE `User`=\"@user\" AND `Identifier`= ?" . " AND delivery.Data=ddts.Data"); foreach $id (@id){ $sth->execute($id); my $found = 0; while (my ($data,$identifier,$user,$version)=$sth->fetchrow_array ) { print "<tr><td><input type=\"checkbox\" name=\"rt\" CHECKED ". "value=\"$identifier\"> $identifier</td></tr>"; $found = 1; } unless ($found){ print "<tr><td><input type=\"checkbox\" name=\"rt\" ". "value=\"$id[$i]\"> $id[$i]</td></tr>"; } } $sth->finish;
Re: mysql database
by injunjoel (Priest) on Dec 12, 2003 at 17:35 UTC
    Greetings all,
    Aside from needing to fix up your query methodology (adopting the use of placeholders "?" would be a good start) you have no else for your if condition. Since there is no else that might be the reason "It's printing all the elements of @id as checked".
    try something like this
    if ($identifier eq $id[$i]){ print "<tr><td><input type=\"checkbox\" name=\"rt\" CHECKED value=\" +$id[$i]\"> $id[$i]</td></tr>"; } else { print "<tr><td><input type=\"checkbox\" name=\"rt\" value=\"$id[$i]\ +"> $id[$i]</td></tr>"; }
    HTH
    -injunjoel