Gossamer Forum
Home : General : Perl Programming :

simple Database count

Quote Reply
simple Database count
Here is my little bit of code sorry very new to perl and just normal copy and paste and find my way through the code

This code works great and pulls out the last 5 records that where added to the database that have a media type of 50

That all works but I want to work out what the total amount of records in the database eg

and out put the total and the last 5 records

eg

total titles in database 155
last 5 entered
title1
title2
title3
title4
title5

Like that But I keep codeing and geting the total is 5 becuase thats what my select query comes back with

So I guess I need to do another call to the SQL database but I am not sure how it adds in to my current code so any help adding
the total amount of records with media type 50 would be great

SELECT
mediatype
FROM
videodata
WHERE
Mediatype <> 50

that would return the list now I just need to count it and fee that in to my script :-) but how and where any help on this would be great becuase Ia m such a perl newbie

Code:

#!/usr/bin/perl

use strict;
use DBI;
use CGI;
use CGI::Carp
qw(fatalsToBrowser);
use HTML::Template;

my $query = CGI->new();

## Define your username/password/SQL server
my $db_server =
"localhost";
my $db_user =
"videodb";
my $db_password =
"password";
my $db_database =
"VideoDB";

my $dbh = DBI->connect(
"dbi:mysql:$db_database:$db_server",$db_user,$db_password) || die("Can't connect");

## Create your SQL call

my $SELECT =
"SELECT imdbID, title, Mediatype, created
FROM videodata
WHERE Mediatype <>
'50'
ORDER BY created
DESC LIMIT 5
";

my $sth = $dbh->prepare($SELECT);

$sth->execute();

my ($imdbID,$title,$MediaType,$created);
$sth->bind_columns(\($imdbID,$title,$MediaType,$created));

my @rows;


while ($sth->fetch) {

push @rows, {
imdbID => $imdbID,
title => $title,
};
}

## Define Template
my $tmpl = HTML::Template->new( filename =>
"sig.tmpl");

## Feed the information you got from the query (@rows) into your template
$tmpl->param(rows => \@rows);

## Print the template
print $tmpl->output;

Quote Reply
Re: [SimonTOZ] simple Database count In reply to
Hi SimonTOZ,

You're correct, you would need a second query to get the total number of records. Here is what I would do:

Code:
#!/usr/bin/perl

use strict;
use warnings;
use DBI;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use HTML::Template;

my $query = CGI->new();

## Define your username/password/SQL server
my $db_server = "localhost";
my $db_user = "videodb";
my $db_password = "password";
my $db_database = "VideoDB";

my $dbh = DBI->connect("dbi:mysql:$db_database:$db_server",
$db_user,$db_password) or die $DBI::errstr;

## Create your SQL call

# Query 1 to get desired records for your loop
my $last_five = q~SELECT imdbID, title, Mediatype, created
FROM videodata
WHERE Mediatype <> '50'
ORDER BY created DESC LIMIT 5~;


# Query 2 to get the total records in DB
my $total = q~SELECT COUNT(*) FROM videodata~;

## Define Template
my $tmpl = HTML::Template->new( filename => "sig.tmpl");

## Feed the information you got from the query (@rows) into your template
$tmpl->param(rows => $dbh->selectall_arrayref($last_five, { Slice => {} }),
count => ($dbh->selectrow_array($total))[0]);

## Print the template
print $tmpl->output;

The selectall_arrayref method called with an empty slice attribute works awesome with HTML::Template for creating your loops.

Hopefully you can catch what I did by reading over the code because I'm a bit too tired right now to explain :) If not just let me know what doesn't make sense.

~Charlie
Quote Reply
Re: [Chaz] simple Database count In reply to
Chaz I just wanted to say thank you so much for helping me with this code as I was pretty much stuck on this and had put it aside for a while.

One problem :-) I should have posted my template file

Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<SIG xmlns="http://www.mydomain.com">
<COUNT><!-- tmpl_var count --></COUNT>
<!-- tmpl_loop rows -->
<MOVIE>
<TITLE><!-- tmpl_var title --></TITLE>
<IMDB>http://www.imdb.com/title/tt<!-- tmpl_var imdbID --></IMDB>
</MOVIE>
<!-- /tmpl_loop -->
</SIG><!-- tmpl_var created -->

As I do not push the created and mediatype
data in to the template, but I am know thinking that I might have the created date but could you show me how to not push them in to the template as the script will error unless I add

<!-- tmpl_var created -->
<!-- tmpl_var mediatype -->

in to the loop on the template
Quote Reply
Re: [SimonTOZ] simple Database count In reply to
Ahh, yes, I should have caught that last night. You have two options:

1) Set die_on_bad_params to false
Code:
my $t = HTML::Template->new(filename => 'sig.tmpl', die_on_bad_params => 0);

2) Change your query to select only the fields you want in the template
Code:
# Query 1 to get desired records for your loop
my $last_five = q~SELECT imdbID, title
FROM videodata
WHERE Mediatype <> '50'
ORDER BY created DESC LIMIT 5~;

I almost always go with option 1 so that neither myself nor the designers I may be working with have to muck with my Perl or SQL code to make simple modifications to the templates.

~Charlie

Last edited by:

Chaz: May 14, 2004, 8:37 AM
Quote Reply
Re: [Chaz] simple Database count In reply to
Thanks Chaz,

# Query 1 to get desired records for your loop
my $last_five = q~SELECT imdbID, title
FROM videodata
WHERE Mediatype <> '50'
ORDER BY created DESC LIMIT 5~;


The query gives me every thing I need I throught it was the correct query to use, my aim was to get the last 5 entered records in the table videodata and to pull out the imdbid value as well as the title and to filter out records that did not have a media value of 50. That was my aim with that SQL query and it works but is it right :-) I attached a copy of the table just in case there was another way to do it.

So I went with option 1 it created the template just fine :-)

Just have to use
my $tmpl rather than my $t


So thank you very much Chaz for the time you spent looking at my newbie code and helping me on this :-)
from these simple code projects I learn so much and I realy appreciate you code kings looking at my simple code and helping me out.



Code:

#
# Structure for the `videodata` table :
#
CREATE TABLE `videodata` (
`id` int(10) unsigned NOT NULL auto_increment,
`md5` varchar(32) default NULL,
`title` varchar(255) default NULL,
`subtitle` varchar(255) default NULL,
`language` varchar(255) default NULL,
`diskid` varchar(15) default NULL,
`comment` varchar(255) default NULL,
`disklabel` varchar(32) default NULL,
`imdbID` varchar(7) default NULL,
`year` year(4) default NULL,
`imgurl` tinytext,
`director` varchar(255) default NULL,
`actors` text,
`runtime` int(10) unsigned default NULL,
`country` varchar(255) default NULL,
`plot` text,
`filename` varchar(255) default NULL,
`filesize` int(16) unsigned default NULL,
`filedate` datetime default NULL,
`audio_codec` varchar(255) default NULL,
`video_codec` varchar(255) default NULL,
`video_width` int(10) unsigned default NULL,
`video_height` int(10) unsigned default NULL,
`istv` tinyint(1) unsigned NOT NULL default '0',
`lastupdate` timestamp(14) NOT NULL,
`seen` tinyint(1) unsigned NOT NULL default '0',
`mediatype` int(10) unsigned NOT NULL default '0',
`custom1` varchar(255) default NULL,
`custom2` varchar(255) default NULL,
`custom3` varchar(255) default NULL,
`custom4` varchar(255) default NULL,
`created` datetime default NULL,
`owner` varchar(255) default NULL,
PRIMARY KEY (`id`),
KEY `title_idx` (`title`),
KEY `diskid_idx` (`diskid`),
FULLTEXT KEY `actors_idx` (`actors`)
) TYPE=MyISAM;