Connecting Tech Pros Worldwide Help | Site Map

IF statement debacle

Newbie
 
Join Date: Dec 2007
Posts: 2
#1: Dec 5 '07
Hello,

recently been working on a form to search an sql db, at the moment its a simple 3 fields but ive been unable to get the if statement working.

all the code is below.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use CGI::Carp "fatalsToBrowser";
  3. use CGI ':standard';
  4. use DBI;
  5. $author=param('author');
  6. $keyword=param('keyword');
  7. $publisher=param('publisher');
  8. $user='user';
  9. $pass='pass';
  10. $info='DBI:mysql:database=user;host=mudfoot';
  11. $dbh=DBI->connect($info ,$user ,$pass) or 
  12. die "Can't_connect_to_database";
  13.  
  14.  
  15.  
  16.  
  17. print header();
  18.  
  19. $query1="SELECT * FROM book where Title='$keyword' AND Author='$author' AND Publisher='$publisher'";
  20. $query2="SELECT * FROM book WHERE Title='$keyword' AND Author='$author'";
  21.  
  22. IF ($publisher = 'ANY') {
  23. $sth=$dbh->prepare($query2); 
  24. }
  25. else
  26. {
  27.  
  28. $sth=$dbh->prepare($query1);
  29. }
  30. $sth->execute;
  31. while(@result=$sth->fetchrow_array)
  32. {
  33.     print "Book Title: $result[0], Publisher: $result[1], Author: $result[2], Quantity in Stock: $result[3], Cost: £$result[4] <BR>";
  34. }
  35.  
  36. $sth->finish;
  37. $dbh->disconnect;
only been using perl around 2 days, so if anyone could advise where i've gone wrong it would be GREATLY appreciated.

Thanks
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#2: Dec 5 '07

re: IF statement debacle


Welcome to TSDN! & Welcome to Perl!

Your if statement should be lowercase. Also, when you are checking a string to see if it is equal to another string then you don't use the = sign you would use eq or for a numeric value it would be ==.
Check out perlop for more.

Expand|Select|Wrap|Line Numbers
  1. if ($publisher eq  'ANY') {
Also, please check out the sticky at the top of this forum.

--Kevin
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#3: Dec 5 '07

re: IF statement debacle


Also, look into declaring your variables. You will find that most of the common problems can be reduce by good coding practices. Do you have a good book that you are learning from?

Some good resources:
perldoc.perl.org
Beginning Perl
CPAN

--Kevin
Newbie
 
Join Date: Dec 2007
Posts: 2
#4: Dec 5 '07

re: IF statement debacle


Thanks alot for the assistance!

I was also wondering if you could suggest a way of doing something in this script for me.

I wish to create a way so that if any of the 3 search terms are entered, for example if just a keyword was entered and the other fields were left blank it would display all records with that keyword in the title. I know this would be possible by doing similar if statements and having multiple queries, but I was wondering if there would be a cleaner way to do this?

Thanks again!
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#5: Dec 5 '07

re: IF statement debacle


This is untested, but the concept is what you are after. If the value of the any of the 3 params are blank then undef is used.

Expand|Select|Wrap|Line Numbers
  1. my $author = param{'author'} || undef;
  2. my $title = param['title'} || undef;
  3. my $publisher = param{'publisher'} || undef;
  4.  
  5. my $sql = $dbh->prepare('SELECT author, title, publisher FROM $table_name WHERE author = ? OR title = ? OR publisher = ?');
  6.    $sql ->execute($author, $title, $publisher);
  7.  
  8. while (my @rows = $sql->fetchrow_array()) {
  9.  
  10.     print join(',', @rows);
  11.  
  12. }
--Kevin
Reply