473,466 Members | 1,298 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

IF statement debacle

2 New Member
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
Dec 5 '07 #1
4 1094
eWish
971 Recognized Expert Contributor
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
Dec 5 '07 #2
eWish
971 Recognized Expert Contributor
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
Dec 5 '07 #3
xand75
2 New Member
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!
Dec 5 '07 #4
eWish
971 Recognized Expert Contributor
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
Dec 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
18
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.