#!/usr/bin/perl use strict; use DBI; use DBD::mysql; my $db_user = ""; # Database Username my $db_pass = ""; # Database Password my $db_name = "test"; # Database Name my $username = "USER1"; my $ordercol = "rec_time"; print "With Order:\n"; my $sql = "SELECT inbox_msg_id, phone_number, rec_time FROM tblinbox WHERE username = ? ORDER BY ? DESC LIMIT 5"; my $dbh = DBI->connect("DBI:mysql:database=$db_name;host=localhost", $db_user, $db_pass, {'RaiseError' => 1}); my $sth = $dbh->prepare($sql); $sth->execute($username, $ordercol); while (my $ref = $sth->fetchrow_hashref()) { print "Found a row: inbox_msg_id = $ref->{'inbox_msg_id'}, phone_number = $ref->{'phone_number'}, rec_time = $ref->{'rec_time'}\n"; } $sth->finish(); print "\n\nWithout Order:\n"; $sql = "SELECT inbox_msg_id, phone_number, rec_time FROM tblinbox WHERE username = ? LIMIT 5"; $sth = $dbh->prepare($sql); $sth->execute($username); while (my $ref = $sth->fetchrow_hashref()) { print "Found a row: inbox_msg_id = $ref->{'inbox_msg_id'}, phone_number = $ref->{'phone_number'}, rec_time = $ref->{'rec_time'}\n"; } $sth->finish(); # Disconnect from the database. $dbh->disconnect();