programming4us
programming4us
WEBSITE

Upgrade Website With Mysql On Linux (Part 2) - Access MySQL from Perl

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
6/20/2012 6:03:27 PM

Access MySQL from Perl

Now you have got the basic concept about how to pull information from a database by using PHP. However, there are times you will want to work with MySQL data outside of the website. For example, if you want to run analyzes of much data in the database or perform automatically administrative tasks based on information in a database, you will not want to do it in PHP. With this type of situations, you will want to use a scripting language, such as Perl.

Description: you can use the MySQL client or you can use a perl API for connecting to MySQL from perl scripts.

you can use the MySQL client or you can use a perl API for connecting to MySQL from perl scripts.

While PHP has an extension installed to interface with MySQL, Perl is much more modular. You have to use many Perl modules before you can start talking to it. In particular, you need to use DBI and DBD::mysql modules of Perl before you can start interacting with MySQL. Fortunately, because MySQL is so popular with open source community, these two modules are usually available with most versions of Linux.

A simple Perl example

In this example, suppose that you want to automatically delete any login account of customers who have not accessed within one year. To do it, you will need a column to track the last time when the customer logged in. Therefore, suppose that our logins database from the PHP example has a column called "last_login". With that information, we can write a Perl script which will delete all user accounts that have not logged in one year. The script could look like this:

#!/usr/bin/perl

use DBI;

use DBD::mysql;

$db_host = "localhost";

$db_user = "root";

$db_pass = "rootpassword";

$db_name = "store"

$dsn = "DBI:mysql:database=$db_

name;host=$db_host";

$dbh = DBI->connect($dsn,

$db_user, $db_pass);

$expire_sql = "SELECT

COUNT(*) AS count

FROM logins WHERE

last_login <= DATE_

SUB(NOW(), INTERVAL

1 year);

$sth = $dbh->prepare("$expire_sql");

$sth->execute;

$row = $sth->fetchrow_hashref;

print "There are " . $row->{"count"} . " accounts that haven’t

been accessed in over a year.\n";

$expire_sql = "DELETE FROM logins WHERE last_login <=

DATE_SUB(NOW(), INTERVAL 1 year);

$sth = $dbh->prepare("$expire_sql");

$sth->execute;

$sth->finish;

$dbh->disconnect;

If you consider PHP code from our previous example, you can see some similarities in the Perl code above. Because the database interface of Perl is more modular than PHP, it has the concept of a Data Source Name (DSN) to indicate which database module of Perl is used. Thus, when you're using Perl to talk to any database, you must specify a DSN to apply to the database you want to talk, with appropriate parameters for that database. Each database has its own DSN format, but the format of the MySQL is just which Perl module, MySQL server is running, and which database is used. Then the code is very similar to PHP code presented above, with the more advanced SQL code are included.

The first $ expire_sql string asks MySQL to count the number of cases where an entry in logins table matches WHERE criteria. In this case, we told MySQL to use preset date function DATE_SUB(), try to reduce one year to the current date, as indicated by NOW (). MySQL will return a number that can be referenced by the count column. We notice how many users we intend to delete and then we create a different SQL query which automatically deletes these columns. After completing the deletion, the script closes the database connection and exit.

Because it’s not enough, we can only cover about it on the website upgrade by adding a MySQL database for it, but hopefully we have given you some basic examples to help you start.

Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Video Sports
- The Banner Saga 2 [PS4/XOne/PC] PC Launch Trailer
- Welkin Road [PC] Early Access Trailer
- 7th Dragon III Code: VFD [3DS] Character Creation Trailer
- Human: Fall Flat [PS4/XOne/PC] Coming Soon Trailer
- Battlefleet Gothic: Armada [PC] Eldar Trailer
- Neon Chrome [PS4/XOne/PC] PC Release Date Trailer
- Rocketbirds 2: Evolution [Vita/PS4] Launch Trailer
- Battleborn [PS4/XOne/PC] 12 Min Gameplay Trailer
- 7 Days to Die [PS4/XOne/PC] Console Trailer
- Total War: Warhammer [PC] The Empire vs Chaos Warriors Gameplay Trailer
- Umbrella Corps [PS4/PC] Mercenary Customization Trailer
- Niten [PC] Debut Trailer
- Stellaris [PC] Aiming for the Stars - Dev. Diary Trailer #1
- LawBreakers [PC] Dev Diary #4: Concept Art Evolutions
programming4us programming4us
programming4us
 
 
programming4us