Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The code should suffice (the following snippet is in a module i call "search.pm". i recognize the code is not "secured". That will come, later...so please, avoid telling me it's insecure - i already know that. i just need this to first work as described in the comments of the subroutine. i wrote this code...and confused the bejeepers outta myself lol
######################## sub sql_execute($$) { #* # runs an SQL statement on the supplied db. # must be connected, and must disconnect to commit changes. # a reference to an array of hash references will be # returned unless there's only one item in the array, then # we give a hash reference instead of a one element array # reference (which would contain only one hash reference). # three different rvals: scalar, array ref, or hash ref # - dependent on # of results, or kind of sql statement (insert, c +reate, update, select, etc) #* my ($dbh, $sql) = @_; # the dbh && the SQL statement if (not sql_db_valid($dbh)) { # sql_db_valid is a VERY simple check +- it just checks if $db is a hash ref or not) return 0; # invalid dbh } my @arr; if ($sql =~ /^insert|update|delete/i) { my $rv = $dbh->do($sql); return $rv; # returns whatever $rv->do($sql) returns } else { my $rv = $dbh->prepare($sql); if ($rv) { $rv->execute(); # now, grab all the results from the query, and dump them into a +n array as hash references to each "hit" while (my $row = $rv->fetchrow_hashref) { push @arr, $row; } # if the array has only one element, then, it's kinda pointless +to return a ref to the array # so instead, let's just return a hash reference. if ($#arr eq 1) { my $hashRef = $arr[0]; # this ought to be a hash reference, no +? return $hashRef; # a hash reference when there is only one arr +ay element } else { if (not @arr) { return 0; # 0 on error } else { # and array reference when there is more than 1 element in t +he array # each element is a reference to a hash. my $arrayRef = \@arr; return $arrayRef; # an array reference when the array is > 1 } # if (not @arr) ... else ... } # if (@arr eq 1) ... else ... } else { return 0; # error in SQL statement! } # if ($rv) ... else ... } # is ($sql =~ /^insert|update|delete/i) #usage: my $rv = sql_execute($dbh, $sql); }
then...in my main script:
. . . use pm::search; my $rv = "content-type: text/html\n\n"; my $dbh = sql_connect("ns.db"); . . . my $st = "eye_clr"; # field to search for my $sv = 1; # value to search for $rv .= "Testing <i><b>search_item</b></i><br>\n"; my @users = get_users($dbh, 1); # gets JUST a list of user ID's - whic +h uses the above "sql_execute" subroutine and works perfectly! $rv .= "searching " . @users . " users: "; foreach my $uid (@users) { $rv .= "$uid, "; } $rv =~ s/, $/<br>\n/; # replace the last comma and space with a <br> a +nd a new line $rv .= ""; # the following should return JUST a hash reference! but nooooo....it +returns convoluted results which just baffle me my @results = search_item($dbh, \@users, $st, $sv) . "\n"; $rv .= "Number of array elements: <b>" . @results . "</b><br>\n"; foreach my $result (@results) { $rv .= "$result<br>\n"; } . . . print $rv; exit 1;
now, when i run this, the "Number of array elements:" says 1 (which is unexpected....i figured i would be getting a hash reference back - not an array reference with one stinkin element in it! and then, what's more confusing? The last "foreach" loop prints "3"! I expected it to print out three user ID's...not just a number....so clearly there's something i'm missing. in some cases this works as I want it to. for instance, i have no issue getting a list of user ID's....that works perfectly. in other cases, though, i get these very strange and unexpected results. can anyone review the above code, and perhaps suggest why i get a "3" and not the expected 3 user ID's??? Jarett

In reply to DB search results using DBI...execute by jamroll

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found