Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Reading from a database

by stephen (Priest)
on Apr 14, 2000 at 02:41 UTC ( [id://7563]=perltutorial: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    use strict;
    
    ...
    # Disconnect from the database
    $dbh->disconnect();
    
  2. or download this
    perldoc DBD::Oracle
    
  3. or download this
    
    # Connect to the database
    my $dbh = DBI->connect('DBI:mysql:my_database', 'my_username', 'my_pas
    +sword')
        or die "Couldn't open database: '$DBI::errstr'; stopped";
    
  4. or download this
    my $sth = prepare(q{SELECT foo FROM bar WHERE baz='bap'})
       or die "Couldn't prepare statement: $DBI::errstr; stopped";
    
  5. or download this
    $sth->execute() or die "Couldn't execute statement: $DBI::errstr; stop
    +ped";
    
  6. or download this
    # Fetch each row and print it
    while ( my ($field1, $field2, $field3) = $sth->fetchrow_array() ) {
         print STDOUT "Field 1: $field1  Field 2: $field2  Field 3: $field
    +3\n";
    }
    
  7. or download this
    # Fetch each row and print it-- fetchrow_arrayref
    while ( my $fields = $sth->fetchrow_arrayref() ) {
         print STDOUT "Field 1: $fields->[0]  Field 2: $fields->[1]  Field
    + 3: $fields->[2]\n";
    }
    
  8. or download this
    # Fetch each row and print it-- fetchrow_hashref
    while ( my $field_hash = $sth->fetchrow_hashref() ) {
         print STDOUT "Field 1: $field_hash->{'field1'}  Field 2: $field_h
    +ash->{'field2'}  Field 3: $field_hash->{'field3'}\n";
    }
    
  9. or download this
    # A bad way of doing it
    while (my $furniture_name = <STDIN>) {
    ...
        my ($price) = $sth->fetchrow_array();
        print STDOUT "Item: $furniture_name Price: $price\n";
    }
    
  10. or download this
    # A better way of doing it
    my $sth = $dbh->prepare("SELECT price FROM furniture_prices WHERE furn
    +iture_name=?")
    ...
        my ($price) = $sth->fetchrow_array();
        print STDOUT "Item: $furniture_name Price: $price\n";
    }
    
  11. or download this
    # One way of reading multiple rows
    my $sth = $dbh->prepare("SELECT furniture_name, price FROM furniture_p
    +rices WHERE furniture_type=?")
    ...
            print STDOUT "Item: $furniture_name Price: $price\n";
        }
    }
    
  12. or download this
    $sth->bind_col($column, \$scalar)
    
  13. or download this
    # Faster way of reading multiple rows
    my $sth = $dbh->prepare("SELECT furniture_name, price FROM furniture_p
    +rices WHERE furniture_type=?")
    ...
            print STDOUT "Item: $furniture_name Price: $price\n";
        }
    }
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-03-19 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found