Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wrote a database searching script

Status
Not open for further replies.

vcherubini

Programmer
May 29, 2000
527
US
Hey guys.

I just got done writing a script that allows a user to search a database, but this one gets down and allows the user to specify what tables and fields to search under.

Right now, you can select multiple tables to search in the database, and search a field below that table.

i.e., if you have a database called forums, and there was a threads field, and below that threads field, you could search a subject field, or something like that.

These scripts are not made to be used by the public, but rather by an admin of a database.

Here they are.

[tt]
# search.pl
#!E:/perl/bin/perl

#----
# made by Vic Cherubini, 2001, krs-one@cnunited.com
# Easily search an entire database on a server.
# Please keep all of this content in here. You can give this out as much as you like
# Any suggestions, tips, or addons you have made, I am always greatful.
# Email them to me at krs-one@cnunited.com
# Thanks!
#----

use strict;
use Mysql;

print "Content-type:text/html\n\n";

# $usedb = use this database as the database to search
my $usedb = "cnunited";
my $host = "localhost";
my $user = "vikter";
my $pass = "morpheus";

my $dbh = Mysql->connect($host,$usedb,$user,$pass);
my @dbs = $dbh->listdbs() unless ($usedb);
my @tables;
push @dbs,$usedb unless (!$usedb);
print &quot;<html><head><title>search</title>\n&quot;;
print &quot;</head><body>\n&quot;;
print &quot;<form action=\&quot;results.pl\&quot; method=\&quot;post\&quot;>\n&quot;;
print &quot;<table border=0 width=100% bgcolor=black cellspacing=1>\n&quot;;
print &quot;<tr><td width=100% bgcolor=silver style=\&quot;padding:5px\&quot;>\n&quot;;
print &quot;<b>Search Text:</b> <input type=\&quot;text\&quot; name=\&quot;query\&quot;></td></tr></table><br><br>\n&quot;;
print &quot;<table border=0 width=100% cellspacing=1 bgcolor=black>\n&quot;;
for my $i (0..$#dbs) {
$dbh->selectdb($dbs[$i]);
push (@tables,$dbh->listtables($dbs[$i]));
print &quot;<tr><td width=100% bgcolor=gray><input type=\&quot;radio\&quot; name=\&quot;database\&quot; value=\&quot;$dbs[$i]\&quot;><b>&quot;.$dbs[$i].&quot;</b></td></tr>\n&quot;;
print &quot;<tr><td width=100% style=\&quot;padding-left:20px;\&quot; bgcolor=silver>\n&quot;;
for my $j (0..$#tables) {
my $query = &quot;SELECT * FROM $tables[$j]&quot;;
my $sth = $dbh->query($query);
my $intnumrows = $sth->numrows();
my $intnumfields = $sth->{NUM_OF_FIELDS};
my @arrfields;
for (my $k=0; $k<$intnumfields; $k++) {
push @arrfields,$sth->{NAME}->[$k];
}
print &quot;<input type=\&quot;checkbox\&quot; name=\&quot;table[$j]\&quot; value=\&quot;$tables[$j]\&quot;><small>$tables[$j] ($intnumrows)</small><br>\n&quot;;
print &quot;     &quot;;
my $k=0;
foreach my $fields (@arrfields) {
if ($k>11) {
$k=0;
print &quot;<br>&quot;;
}
print &quot;<input type=\&quot;radio\&quot; name=\&quot;table[$j]field[$k]\&quot; value=\&quot;$fields\&quot;>$fields&quot;;
$k++;
}
print &quot;<br>&quot;;
}
print &quot;</td></tr>\n&quot;;
@tables = ();
}
print &quot;</table><br>\n&quot;;
print &quot;<input type=\&quot;submit\&quot;>\n&quot;;
print &quot;</form></body></html>\n&quot;;
[/tt]

And

[tt]
# results.pl
#!E:/perl/bin/perl


#----
# made by Vic Cherubini, 2001, krs-one@cnunited.com
# Easily search an entire database on a server.
# Please keep all of this content in here. You can give this out as much as you like
# Any suggestions, tips, or addons you have made, I am always greatful.
# Email them to me at krs-one@cnunited.com
# Thanks!
#----

use strict;
use Mysql;

print &quot;Content-type:text/html\n\n&quot;;
check_method();

my %FORM = parse_form();
check_query($FORM{'query'});

print &quot;Search on database: <b>&quot;.$FORM{'database'}.&quot;</b> with query \&quot;&quot;.$FORM{'query'}.&quot;\&quot;.<br><br>&quot;;
my $host = &quot;localhost&quot;;
my $user = &quot;vikter&quot;;
my $pass = &quot;morpheus&quot;;

my $dbh = Mysql->connect($host,$FORM{'database'},$user,$pass);
my @arrtables = $dbh->listtables([$FORM{'database'}]);
for my $i (0..$#arrtables) {
my $query = &quot;SELECT * FROM &quot;. $FORM{'table['.$i.']'}.&quot;&quot;;
my $sth = $dbh->query($query);
my $intnumfields = $sth->{NUM_OF_FIELDS};
for (my $j=0; $j<$intnumfields; $j++) {
my $field = $FORM{'table['.$i.']field['.$j.']'};
my $sqlquery = &quot;SELECT * FROM &quot;.$FORM{'table['.$i.']'}.&quot; WHERE &quot;. $FORM{'table['.$i.']field['.$j.']'} .&quot; LIKE '%&quot;.$FORM{'query'}.&quot;%'&quot;;
unless (!defined($FORM{'table['.$i.']field['.$j.']'})) {
my $sth2 = $dbh->query($sqlquery);
my $numrows = $sth2->rows();
print &quot;Table: <b>&quot;.$FORM{'table['.$i.']'}.&quot;</b><br>&quot;;
print &quot;     Field: <b>&quot;.$FORM{'table['.$i.']field['.$j.']'}.&quot;</b><br>&quot;;
for (my $k=0; $k<$numrows; $k++) {
my $hashref = $sth2->fetchrow_hashref();
$hashref->{$field} =~ s/\</</g;
$hashref->{$field} =~ s/\>/>/g;
if (get_result_size($hashref->{$field}) >= 128) {
$hashref->{$field} = get_snippet($hashref->{$field});
}
my $temp += $k + 1;
print &quot;        $temp. <i><small>&quot;.$hashref->{$field}.&quot;</small></i><br>&quot;;
}
print &quot;Found $numrows results.<br>&quot;;
print &quot;<hr>&quot;;
}
}
}

sub parse_form {
my ($buffer,$pair,$name,$value,%FORM,@pairs);
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
if ($FORM{$name}) {
$FORM{$name} = &quot;$FORM{$name}, $value&quot;;
} else {
$FORM{$name} = $value;
}
}
return %FORM;
}

sub check_method {
if ($ENV{'REQUEST_METHOD'} eq &quot;GET&quot;) {
print &quot;This page only allows POST request methods&quot;;
die;
}
}

sub get_result_size {
my $result = shift;
return length($result);
}

sub get_snippet {
my $text = shift;
return substr($text,0,128).&quot; . . . &quot;;
}

sub check_query {
my $query = shift;
if (!$query) {
print &quot;You need to specify a query to search for.&quot;;
die;
}
}
[/tt]

Save the first on as search.pl, and the other one as results.pl.

I just wrote these today, so I am sure they are pretty bug filled, but, if anyone wants to play around with them and report back what you like and don't like, I am always appreciative.

I am sure the forum sages could pick them apart piece by piece, and thats cool, it can only help.

In case those don't work, you can download them here:


and in case that does not work, you can always email me at krs-one@cnunited.com, and I can email them to you.

Thanks a lot and have fun messing with the scripts, I think you will like them.

-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
Haven't really taken a look at the code yet, but one thing catches my attention: the dbname, username and password data that you have in there isn't correct, is it? :)

brendanc@icehouse.net
 
What do you mean correct? The host is localhost, so even if it were correct, there would be now way to access it.

Nah, its an old database that I was working on a while back.

-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top