Jump to content

database query problem

- - - - -

  • Please log in to reply
4 replies to this topic

#1
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
please experts, I want to query and print my database records on a web page, but
when I run the code it displays nothing. below is the work so far




#!C:/Ruby192/bin/ruby 


require "dbi"

require 'cgi'


cgi = CGI.new

puts cgi.header







begin

     # connect to the MySQL server

     dbh = DBI.connect("DBI:Mysql:testdb:localhost", 

	                    "root", "root77")

     sth = dbh.prepare("SELECT * FROM data")

     sth.execute()


     sth.fetch do |row|

        printf "First Name: %s, Last Name : %s\n", row[0], row[1]

        printf "Sex : %s\n", row[2]

 printf "Salary :%s \n\n", row[3]

       

     end

     sth.finish



rescue DBI::DatabaseError => e

     puts "An error occurred"

     puts "Error code:    #{e.err}"

     puts "Error message: #{e.errstr}"

ensure

     # disconnect from server

     dbh.disconnect if dbh

end



thanks

#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
Do you have data in the table?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
yes, there is data in the table

#4
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
I'm not a Ruby coder, but this looks promising: Using the Ruby DBI Module
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
mutago

mutago

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
thanks, is working




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users