Hi, need to store the results of an SQL search either in a file or a variable for use later on. I had to apply a regex to the search results to filter out on specific criteria and display in a table on the browser. My problem is storing the results of the table into a variable or logfile so I can search on those results later on. Code just now is:
CODE
$sql = ("SELECT title, name, surname,
year, bids FROM book");
$sth=$dbh->prepare($sql);
$sth->execute() ||
die "Could not execute SQL statement... maybe invalid syntax?";
#show the results of the regex query in a table
print "<br>Search Results:<br>".
"<table border=1>";
while (my @array = $sth->fetchrow_array()){
print "<tr>";
foreach my $elem (@array){
#search & display the first element in the array for the regex
$searchTitle=$array[0];
if ($searchTitle=~/(?=.*pr\w{4}m)(?=(?=\w*w\w*)(?=\w*b\w*)\w+\b)/){
print "<td>";
print "$elem\n";
print "<td>";
}
}
}
print "</table>";
Up to here is fine, and the program does what i need it to do, which is display the results of the regex in a table. Ive tried printing the results to a logfile as follows:
CODE
$sql = ("SELECT title, name, surname,
year, bids FROM book");
$sth=$dbh->prepare($sql);
$sth->execute() ||
die "Could not execute SQL statement... maybe invalid syntax?";
#show the results of the regex query in a table
print "<br>Search Results:<br>".
"<table border=1>";
while (my @array = $sth->fetchrow_array()){
print "<tr>";
foreach my $elem (@array){
#search & display the first element in the array for the regex
$searchTitle=$array[0];
if ($searchTitle=~/(?=.*pr\w{4}m)(?=(?=\w*w\w*)(?=\w*b\w*)\w+\b)/){
print "<td>";
print "$elem\n";
print "<td>";
#PROBLEM IS HERE ONWARDS: This prints out the title element n times where n =number # of columns in the SQL search
unless (open (LOGFILE, ">>//logs/search1results"))
{print "Failed to open search1results log!";exit}
flock (LOGFILE, LOCK_EX);
print LOGFILE "$searchTitle\n";
unless (close (LOGFILE))
{print "Failed to close search1results log!";exit}
}
}
}
print "</table>";
But the first element of the array gets printed out once each time the program searches a new column. I just need one line for each regex result. Im not sure if the code is right, or if its just in the wrong place (ive tried it in various places, but this seems to work the best), or is there an easier way to store the regex in a variable somehow?