programming4us
programming4us
WEBSITE

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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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
Free Mobile And Desktop Apps For Accessing Restricted Websites
MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
- Messages forwarded by Outlook rule go nowhere
- Create and Deploy Windows 7 Image
- How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
- Creating and using an unencrypted cookie in ASP.NET
- Directories
- Poor Performance on Sharepoint 2010 Server
- SBS 2008 ~ The e-mail alias already exists...
- Public to Private IP - DNS Changes
- Send Email from Winform application
- How to create a .mdb file from ms sql server database.......
programming4us programming4us
programming4us
 
 
programming4us