use DBI;
my $DB = DBI->connect( ... connection info goes here ...);
my $sql="SELECT * FROM addressbook WHERE deprecated='0000-00-00 00:00:
+00' AND thumbPhoto IS NOT NULL";
my $recordset = $DB->prepare($sql);
$recordset->execute();
while (my $row=$recordset->fetchrow_hashref){
open my $handle, ">", "./images/$row->{photo}";
my $data = $row->{thumPhoto};
print $handle, $row->{thumbPhoto};
}
As you can see, the code is very similar. There's a bit of syntax difference between how perl and php access a hash
Notes:
- As you can see, the code is very similar, though there are a few differences. None of the differences are particularly alarming, however:
- The syntax to access a hash from perl is slightly different than for PHP.
- Since we're using a different module for database access, the code to set up the statement and fetch the results are similar but different.
- Similarly, file handling is a bit different beteen perl and PHP, so opening the file and writing the file are a bit different as well.
- I did a translation of code, not intent. You may need to tweak things based on your database definitions, environment, operating system, etc.
- I didn't add any error handling, so you'll want to add some if/when you use it.
- The DBI database interface package is probably (to me) the most useful module. Other database interface modules exist, and some may be more similar to the mysql_ stuff used in php. But if you use DBI, I don't think you can go wrong.
- In DBI the prepare method lets you set up a statement so you can use parameters, which is why it's separate from the execute method. If you do any significant database work, learn to use prepared statements no matter which language/platform you wind up using. You'll be happier that way.
- All done from the top of my head, with no ability to test it. (My current $work PC has neither perl nor DB access any longer.) With luck, I haven't farbulated anything inquoherently.
- Since perl will close the file handle $handle when it goes out of scope, I didn't bother explicitly closing the file.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.