In regards to a SOPW a few days ago (closely related but you don't need to read it to understand this problem).

I have a MySQL table "visit" that has colums: id, engine_id, useragent, ip, date. What's the easiest way to retrieve the data where you can print/manipulate it easily?

Right now I am using the below code but it only retrieves ONE record for some reason when there is 20-30 in the database.

my $useragent = "SELECT useragent FROM visit WHERE engine_id = 'Junk'"; $sth = $dbh->prepare($useragent); $sth->execute() or die $dbh->errstr; my @row = $sth->fetchrow_array; foreach (@row) { print "<center>$_</center>"; }
You notice I am only printing back the useragent right now. This is because I don't understand how I could pull back all the needed columns at one time and be able to manipulate them freely.

Using the following code everything I need is in one glob of data @row. How am I supposed to created HTML tables in such a specific order when it's one massive variable?

my $data = "SELECT engine_id,useragent,ip,date FROM visit WHERE engine_id = 'Junk'"; $sth = $dbh->prepare($data); $sth->execute() or die $dbh->errstr; while (my @row = $sth->fetchrow_array) { print "$_<br>" for @row }
One idea I had was demonstrated with the first code sample. I could run through all that code each time for each column and store it in @row1, @row2, @row3 etc. But that seems to be a ton of extra coding which is needed for something as ordered data printing like this. And then you'd have to worry about making sure all the variables' data matches up when you display it on the screen.

In short what I'm trying to do is print in a format like:

<td>COUNT (ID)</td> <td>UserAgent</td> <td>IP</td> <td>time</td> <td>< +/td><tr> ...... again ....
As much as I'd like code to do just that I'd really appreciate also having an explanation of what you did. I'd much rather understand it so I don't have to ask this again next time I do something like this.

Thank you everyone.


In reply to printing records in MySQL by coldfingertips

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.