#!/usr/bin/perl
# Get the environment variables passe to the script
use DBI;
use CGI qw/:standard :html3 :netscape/;
use LWP::Simple qw/get/;
$db_user = "agentrobin_01_";
$db_pass = "J0shKr1s";
$dsn_name = 'dbi:mysql:dreamhome_01_:dobrooks.ipowermysql.com:3306';
### Get the search string ####
@FormValues = split(/&/, $ENV{QUERY_STRING});
$FV1 = @FormValues;
for ($x=0;$x<=$FV1-1;$x++) {
@Attributes = split(/=/,$FormValues[$x]);
if ($Attributes[0] eq "QS") {
$QS=$Attributes[1]; $QS =~ tr/+/ /; $QS =~ s/%([1-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
}
}
printheader();
### Get teh Serach URL
$QS1=$ENV{'QUERY_STRING'}; $QS1 =~ tr/+/ /; $QS1 =~ s/%([1-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
@vals=split(/"/,$QS1);
$SQ1 = $vals[1];
$SURL = "http://www.danvillelive.com/quicktest.pl?QS=\"$SQ1\"";
print "$SURL\n";
### Get the webpage with the results
$content = get $SURL; #put site html in $content.
die "get failed" if (!defined $content);
### Print the information out.
@lines = split(/\n/, $content);
$linecount=@lines;
$nextline ="N";
$bodyreached = 0;
for($z=1;$z<=$linecount-1;$z++) {
if (length($lines[$z]) > 100) {
$lines[$z] =~ s/'//g;
$lines[$z] =~ s/\(//g;
$lines[$z] =~ s/\)//g;
@data=split(/,/,$lines[$z]);
print "$data[0]<br>\n";
}
}
printending();
exit(0);
##############################################################################
################### GENERIC PAGE SETUP HEADING AND METADATA ###################
##############################################################################
sub printheader() {
$description = "AgentRobin Tri-valley Real Estate";
# print "<!DOCTYPE html '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>";
print "Content-type: text/html\n\n";
print "<HTML xmlns='http://www.w3.org/1999/xhtml'>\n";
print "<HEAD>\n";
print "<TITLE>HELLO</title>\n";
print "</head>\n";
print "<body>\n";
}
################### GENERIC PAGE SETUP ENDING ###################
sub printending {
print "</body>\n";
print "</html>\n";
}
sub get_data() {
$SQL = @_[0];
$dbh = DBI->connect('dbi:mysql:dreamhome_01_:dobrooks.ipowermysql.com:3306','agentrobin_01_','J0shKr1s') or die("Couldn't connect");
$sth = $dbh->prepare($SQL);
$sth->execute;
$rows = $sth->rows();
for ($i = 0; $i < $rows; $i++)
{
@data=$sth->fetchrow_array();
$Pstr="";
for ($x=0; $x<=44; $x++) {
if ($data[0] eq "") { goto DONTCOUNT; }
if (length($data[0]) < 8 || length($data[0]) > 8) { goto DONTCOUNT; }
$export_line ="(";
for ($x=0;$x<=44;$x++) {
$export_line = $export_line . "'$data[$x]',"
}
$zlen = length($export_line);
$new_line = substr($export_line,0,$zlen-1) . ")";
$new_line =~ s/%([1-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$new_line =~ s/[\x0A\x0D]//g;
print "$new_line<br>\n";
BLANK:
$count++;
DONTCOUNT:
$countall++;
}
}
}
sub db_sql
{
$sql = "SELECT * FROM PropMLS";
$dbh = DBI->connect('dbi:mysql:dreamhome_01_:dobrooks.ipowermysql.com:3306','agentrobin_01_','J0shKr1s') or die("Couldn't connect");
if (! defined($dbh) )
{
print "Error connecting to DSN '$dsn_name'\n";
print "Error was:\n";
print "$DBI::errstr\n"; # $DBI::errstr is the error
# received from the SQL server
return 0;
}
$sth = $dbh->prepare("GO");
$sth->execute; # Execute the "GO" statement
$sth->finish; # Tell the SQL server we are done
$sth = $dbh->prepare($sql); # Prepare the SQL statement passed to db_sql
#
# Check that the statement prepared successfully
#
if(! defined($sth) || ! ($sth))
{
print "Failed to prepare SQL statement:\n";
print "$DBI::errstr\n";
#
# Check for a connection error -- should not occur
#
if ($DBI::errstr =~ /Connection failure/i)
{
if (! dbh_connect() )
{
print "Unable to connect to database.\n";
exit; # Exit gracefully
}
else
{
print "Database connection re-established, attempting to prepare again.\n";
$sth = $dbh->prepare($sql);
}
}
#
# Check to see if we recovered
#
if ( ! defined( $sth ) || ! ($sth) )
{
print "Unable to prepare SQL statement:\n";
print "$sql\n";
return 0;
}
}
#
# Attempt to execute our prepared statement
#
$rc = $sth->execute;
if (! defined( $rc ) )
{
#
# We failed, print the error message for troubleshooting
#
print "Unable to execute prepared SQL statement:\n";
print "$DBI::errstr\n";
print "$sql\n";
return 0;
}
#
# All is successful, return the statement handle
#
return $sth;
}
sub retrieve_rows
{
my ($sql, $sth, $href);
$sql = "SELECT * FROM PropMLS";
$sth = db_sql( $sql ); # Pass the SQL statement to the server
#
# Check that we received a statement handle
#
if (! ($sth) )
{
return 0;
}
#
# Retrieve the rows from the SQL server
#
while( $href = $sth->fetchrow_hashref )
{
print "ID: " . $$href{"MLSID"} . "\n";
print "FOO: " . $$href{"FOO"} . "\n";
print "BAR: " . $$href{"BAR"} . "\n";
print "<br>\n";
}
return 1;
}