我的CGI脚本打印MySQL数据有什么问题?(What is wrong with my CGI script to print MySQL data?)
我想使用Ruby CGI脚本在浏览器中显示MySQL数据库中的数据。
我遇到的问题是显示数据; 它仅显示“标题”列,并且仅显示一个单元格,而不显示“价格”和“ISBN”列。
我使用“标题varchar,价格小数(10,2),ISBN整数”来创建表格。
我首先尝试显示价格和ISBN,但这两列甚至不打印,但数据在数据库中。
#!/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>";I want to display data from my MySQL database in a browser, using a Ruby CGI script.
The problem I have is displaying the data; It only displays the Title column, and for only one cell and nothing for the Price and ISBN columns.
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 don't even print, 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>";最满意答案
修正了“#{row [”Price“]。to_f}”。 谢谢!
Fixed it with "#{row["Price"].to_f}". Thanks!
更多推荐
发布评论