I
I
Igor Tkachenko2015-03-18 02:31:10
Perl
Igor Tkachenko, 2015-03-18 02:31:10

How to get array key and value from select in perl via DBI?

I have been working with perl for the 2nd day, so do not swear, I googled, I really did not understand anything.
There is a code, actually how do I get the key-> value from the array selected from the database, for example $row['name'] == 'name'.

#!/usr/bin/perl
use DBI;
use threads;
use Data::Dumper;
my $ds = 'DBI:mysql:*:localhost';
my $user = 'root';
my $passw = '*';

my $db = DBI->connect($ds, $user, $passw);

my $res = $db->prepare("SELECT * FROM jobs");


$res->execute();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pcdesign, 2015-03-18
@pcdesign

It can be like this:

my $n = $dbh->prepare('SELECT * FROM `table`  ');
$n->execute();
my $all = $n->fetchall_arrayref( {} );
foreach my $item (@$all) {
    print $item->{'name'};
}

Option from the comments from kloppspb
my $all = $dbh->selectall_arrayref( "SELECT * FROM table ", { Columns => {} } );
for my $item (@$all) {
    print $item->{'name'};
}

K
krypt3r, 2015-03-18
@krypt3r

In DBI, there are a lot of methods for selecting from tables, take a look.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question