ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a perl script that searches for a pattern within a mysql database but the search is not working right. Here are pieces of the script that I think need improvement. Line 1 and 2 work fine. Line3 does not print out the results but the $search variable is displayed ok in Line4.
#line1
print "Enter search string:>;
#line2
my $search = <STDIN>;
#line3
my $query="SELECT id,problem,resolution FROM testtable where problem like '%$search%'";
#line4
print "The search results in $search are:\n";
If change #line3 to include the actual search string without defining a varible, the search works great.
my $query="SELECT id,problem,resolution FROM testtable where problem like '%searchstring%';
Is line3 not interpreting the search variable correctly?
Additionally you will need to use chomp to remove the carriage return from the string $search
Code:
#!/usr/bin/perl
use strict;
print "Enter search string:";
my $search;
chomp($search = <STDIN>);
my $query="SELECT id,problem,resolution FROM testtable where problem like '%" . $search . "%'";
print "The search results in $search are:\n";
Last edited by Disillusionist; 07-10-2009 at 11:41 AM.
#!/usr/bin/perl
use DBI;
use strict;
use warnings;
#Prompt user to enter search string
print "Enter search string:\n";
my $search;
#Chomp removes carriage return from the string
$search chomp($search = <STDIN>);
# database information
my $db="testdb";
my $host="localhost";
my $port="3306";
my $userid="blah";
my $passwd="blah";
my $connectionInfo="DBI:mysql:database=$db;$host:$port";
# make connection to database
my $dbh =
DBI->connect($connectionInfo,$userid,$passwd);
# prepare and execute query
my $query="SELECT id,problem,resolution FROM testtable where problem like '%" . $search . "%'";
my $sth = $dbh->prepare($query);
$sth->execute();
# assign fields to variables
my ($id,$problem,$resolution);
$sth->bind_columns(undef, \$id, \$problem, \$resolution);
# output search and resolution
print "The search results are:\n";
while($sth->fetch()) {
print "$problem ";
print "$resolution\n";
}
# clean up
$sth->finish();
# disconnect from database
$dbh->disconnect;
You could try looping through, with a slight change to your code:
Code:
#!/usr/bin/perl
use DBI;
use strict;
use warnings;
my $search;
#Prompt user to enter search string
print "Enter search string:\n";
#Chomp removes carriage return from the string
$search chomp($search = <STDIN>);
if ($search) {
# database information
my $db="testdb";
my $host="localhost";
my $port="3306";
my $userid="blah";
my $passwd="blah";
my $connectionInfo="DBI:mysql:database=$db;$host:$port";
# make connection to database
my $dbh =
DBI->connect($connectionInfo,$userid,$passwd);
while ($search) {
# prepare and execute query
my $query="SELECT id,problem,resolution FROM testtable where problem like '%" . $search . "%'";
my $sth = $dbh->prepare($query);
$sth->execute();
# assign fields to variables
my ($id,$problem,$resolution);
$sth->bind_columns(undef, \$id, \$problem, \$resolution);
# output search and resolution
print "The search results are:\n";
while($sth->fetch()) {
print "$problem ";
print "$resolution\n";
}
# clean up
$sth->finish();
print "Enter search string:\n";
#Chomp removes carriage return from the string
$search chomp($search = <STDIN>);
}
# disconnect from database
$dbh->disconnect;
}
This should loop through asking for a new search string.
To exit just press [Return] on it's own.
Last edited by Disillusionist; 07-10-2009 at 02:49 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.