Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

if statement problem using mysql

by CreationP (Initiate)
on May 31, 2012 at 14:55 UTC ( #973540=perlquestion: print w/replies, xml ) Need Help??
CreationP has asked for the wisdom of the Perl Monks concerning the following question:

I'm using a table named 'options' which has 1 field with the name 'log' and one record which can be only 1 or 0. Default value is 1. I want to enable or disable logging to my progra, using the following code:

if ($choise ==6){ $sql1 = "select * from scanner.options"; $sth1 = $dbh ->prepare($sql1); $sth1->execute or die "SQL Error: $DBI::errstr\n"; while (@option - $sth1 ->fetchrow_array()){ $log = $option[0]; } if ($log == 1){ print "Logs = 1"; print "\n\nWould you like to disable logging? (press 1 for yes + or anything else for no): "; my $o_log= <STDIN>; if ($o_log == 1){ my $sql = "UPDATE options SET log=0 WHERE log=1"; $sth = $dbh->prepare($sql); $sth->execute or die "SQL Error: $DBI::errstr\n"; } } else{ print "Logs = 0"; print "\n\nWould you like to enable logging? (Press 1 for yes +or anything else for no): "; my $o_log= <STDIN>; if ($o_log == 1){ my $sql = "UPDATE options SET log=1 WHERE log=0"; $sth = $dbh->prepare($sql); $sth->execute or die "SQL Error: $DBI::errstr\n"; } } }

Using this code I cannot get the program to go into the first if function ($log == 1). I also tried with if ($option[0] == 1) and it still does not work. My value in the mysql table is 1. Why is it not working and how can I make it work? Thank your for your time.

Replies are listed 'Best First'.
Re: if statement problem using mysql
by Eliya (Vicar) on May 31, 2012 at 15:05 UTC
    while (@option - $sth1 ->fetchrow_array()){ ^

    Is this a typo?

      Dear Eliya, No it wasn't a typo. I don't know why but I thought it was the correct form. I changed the '-' to '=' and worked as intended. Thank you very much for scanning my code and finding out the error. You guys here in perlmonks are lifesavers!

Re: if statement problem using mysql
by toolic (Bishop) on May 31, 2012 at 15:04 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://973540]
Approved by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2016-10-21 00:39 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (284 votes). Check out past polls.