Jump to content

if-else database problem

- - - - -

  • Please log in to reply
2 replies to this topic

#1
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
Good day everyone, i want to print a value from database using if-else statement
if things are okay. but don't know how to proceed
below is the code


#!C:/Ruby192/bin/ruby 

require 'cgi'


cgi = CGI.new

puts cgi.header


require "mysql"


begin

    con = Mysql.new 'localhost', 'root', 'mak44', 'testdb'


    rs = con.query("SELECT * FROM records")

    

    rs.each do |row|

        #print value of price in records table

   puts row[0]

end

if row[0] == '100'  

        puts 'yes'

else 

	puts 'Hello '

end

         

rescue Mysql::Error => e

    puts e.errno

    puts e.error

    

ensure

    con.close if con

end


the code prints the value of price but does not print yes from the if statement if the value of price from database is 100

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It looks like your if statement is outside your do loop.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
yes, that is the problem.the code is now working. thanks a lot
Mr. Winged Panther




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users