Suggestion for your first question: You don't seem to be using all the rows that are returned from your query, so first, rather than selecting '*' you should select only the columns you want by name (since I don't know your column names this is only an example)

SELECT column1,column2,column3 FROM table WHERE column4=?";

Second suggestion has to do with the '?' on the end of my example. This is called a place holder and you should always use them. Why? Because DBI will then escape any bad characters for you. Also using this method your query will be cached and you can call it will lots of different parameters more than once without rebuilding the query, saving on coding time and speed for your program. (they are great in loops). You add the parameters by passing what you want in that question mark with the execute statement.
A 'gotcha' of placeholders however is that you can only use them to represent values, you can't use them to represent table or column names or other pieces of a statement. (in other words something like this:

SELECT ? FROM ? WHERE ?=?
would not work).

Finally, instead of using $sth->fetchrow_array() use $sth->fetchrow_hashref(). This will return a reference to the hash you're looking for. The keys to this hash are your column names. So your final code would look something like this:

my $query = "SELECT column1,column2,column3 FROM table WHERE column4=? +"; my $sth = $dbh->prepare($query); $sth->execute($value_for_placeholder) or die $sth->errstr(); my $hash_ref = $sth->fetchrow_hashref(); # if its easier for you, you can dereference that hash_ref: my %hash = %$hash_ref;

Voila! and there you have it. I hope that helps!

Chris

Lobster Aliens Are attacking the world!

In reply to Re: Reading data from mySQL into hashes and manipulation of hashes by cfreak
in thread Reading data from mySQL into hashes and manipulation of hashes by lagrenouille

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.