Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Getting MySQL Data into an Array.

by coolmichael (Deacon)
on Jan 06, 2002 at 15:24 UTC ( #136671=note: print w/ replies, xml ) Need Help??


in reply to Getting MySQL Data into an Array.

You are very close. The problem is that you first have to execute the query, which in itself does not return any results. After you execute the query, you then fetch the results, one row at a time. I'm not aware of a way to get all the results at once, sorry. Perhaps another monk can enlighten? For more information, see perldoc DBI and also references, or try this:

$dsn = "DBI:mysql:yip;db.yip.com"; $dbh = DBI->connect($dsn,'me','pass'); $sth = $dbh->prepare("SELECT company FROM company_public;"); $sth->execute(); while($ref = $sth->fetchrow_arrayref) { push @companies, $ref->[0]; } $dbh->disconnect;

update: added the semicolon. Thanks Parham. </code>


Comment on Re: Getting MySQL Data into an Array.
Select or Download Code
Re: Re: Getting MySQL Data into an Array.
by Parham (Friar) on Jan 06, 2002 at 21:22 UTC
    i think coolmichael explained is pretty well... but if you want to dump all your information into an array before starting to work and avoiding a while loop altogether, try this:

    note: coolmichael, you forgot the semicolon at the end of your code ;)
    $dsn = "DBI:mysql:yip;db.yip.com"; $dbh = DBI->connect($dsn,'me','pass'); $sth = $dbh->prepare("SELECT company FROM company_public;"); $sth->execute(); $companies = $sth->fetchall_arrayref; #a reference to an array of arra +ys of references to each row $dbh->disconnect; foreach $row (@$companies) { ($company) = @$row; print "$company\n"; #just printing it out for fun }
    if your starting out new with mysql and perl... this tutorial should help you a lot: perlguy

      There are some nitpicks I do have with this tutorial.

      First of all, it's the absence of use strict;, which is not really good in code that will be cut, pasted and modified, possibly with typos.

      The mix of implicit return values and explicit return ($sth) statements is also likely to bother the newbie.

      The author also seems to never have heard of the quote method (see http://mysql.turbolift.com/mysql/DBD_3.21.X.php3#quote for example). I'm not sure if this method is available under every database driver, but it's at least worth a mention.

      Another problem comes from the naive error-handling approach when mixing CGI and the rest of the world - the author prints the full error message back to the end user, something which will at best confuse the end user, in the worst case this will provide crucial information to an attacker. Of course, the topic of the tutorial is databases and not CGI, but why mix in CGI in the first place ?

      perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
        I won't disagree with you Corion because "perlguy" does in fact do a few things incorrect. The filter() and Do_SQL() subroutines don't seem all too practical and 'clear'. On the other hand, "perlguy" understandably explains how to SELECT, INSERT, DELETE and UPDATE database entries which i believe were the key points of the tutorial. It is one of the few (only one i found) tutorials which give you an entire program to disect with line-by-line explanations of what is going on.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://136671]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2014-09-19 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (137 votes), past polls