我的CGI脚本打印MySQL数据有什么问题?(What is wrong with my CGI script to print MySQL data?)

编程入门 行业动态 更新时间:2024-10-24 08:31:27
我的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!

更多推荐

本文发布于:2023-04-27 16:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327189.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么   脚本   数据   MySQL   CGI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!