Tuesday, 3 September 2013

ruby cgi script to print mysql data

ruby cgi script to print mysql data

I want to display my sql data on a browser using a ruby cgi script Problem
I have is displaying the data, it only displays Title column and for only
one cell and nothing for Price and ISBN.
I used "Title varchar, Price decimal(10,2), ISBN integer" to create the
table I tried displaying Price and ISBN first but those two columns doesnt
even print anything but the data is in the database.
#!/usr/bin/env ruby
require 'mysql2'
require 'cgi'
client = Mysql2::Client.new(
:host => "localhost",
:database => "tempdb",
:username => "user",
:password => "pass"
)
results = client.query("SELECT * FROM mytable")
cgi = CGI.new
puts cgi.header
puts "<table border='1'>
<tr>
<th>Title</th>
<th>Price</th>
<th>ISBN</th>
</tr>"
results.each do |row|
puts "<tr>"
puts "<td>" + row["Title"] + "</td>"
puts "<td>" + row["Price"] + "</td>"
puts "<td>" + row["ISBN"] + "</td>"
puts "</tr>"
end
puts "</table>";

No comments:

Post a Comment