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.